Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GUI Java字符串转换为地址_Java_String_User Interface - Fatal编程技术网

GUI Java字符串转换为地址

GUI Java字符串转换为地址,java,string,user-interface,Java,String,User Interface,我正在尝试通过GUI输入添加客户。用户输入由下面显示的部分组成的地址,这将成为defaultColAddress。有一个问题,因为defaultColAddress必须是Address,但组成它的组件是String,我正在尝试将它们转换为地址,但无法解决问题。问题行是: Address defaultColAddress = Address.parseAddress(addrLine1, addrLine2, city, postcode); 我在Address.parseAddress上出错

我正在尝试通过GUI输入添加客户。用户输入由下面显示的部分组成的地址,这将成为defaultColAddress。有一个问题,因为defaultColAddress必须是Address,但组成它的组件是String,我正在尝试将它们转换为地址,但无法解决问题。问题行是:

Address defaultColAddress = Address.parseAddress(addrLine1, addrLine2, city, postcode);
我在Address.parseAddress上出错。我知道没有对Address进行解析,但我也尝试了所有变体,如toAddress等

任何帮助都将不胜感激

 private void saveItem()
    {
        if(this.isSaveable()){
            double costPerKg = Integer.parseInt(txtDefaultCost.getText());
            String addrLine1 = txtAddressLine1.getText();
            String addrLine2 = txtAddressLine2.getText();
            String city = txtCity.getText();
            String postcode = txtPostcode.getText();
            Person itemToAdd = null;
            Address defaultColAddress = Address.parseAddress(addrLine1, addrLine2, city, postcode);
            //Address defaultColAddress = Address.parseAddress(txtAddressLine1.getText(),txtAddressLine2.getText(),txtCity.getText(), txtPostcode.getText());

                itemToAdd = new Customer(txtForename.getText(),txtSurname.getText(), costPerKg, defaultColAddress);
                peopleList.addCustomer((Customer) itemToAdd);
        }
    }

    private Boolean isSaveable()
    {
        Boolean blnValid = false;
        try
        {
            Integer intQty = Integer.parseInt(txtDefaultCost.getText());
            if (txtForename.getText().length() > 0 && intQty >= 0)
            {
                blnValid = true;
            }
        } catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "The provided data is invalid", "Error reading data", JOptionPane.ERROR_MESSAGE);
        }
        return blnValid;
    }
这是请求的整个Address类

公共类地址实现ISubject,可序列化{

private String addressLine1;
private String addressLine2;
private String city;
private String postcode;

private ISubject subjectDelegate;

/**
 * Default constructor initialises all attributes to the string "UNKNOWN".
 * Required for serialisation
 */
public Address(){
    this.subjectDelegate = new ISubjectImpl();
    this.addressLine1 = "UNKNOWN";
    this.addressLine2 = "UNKNOWN";
    this.city = "UNKNOWN";
    this.postcode = "UNKNOWN";
}

/**
 * Constructor that initialises the object with the provided address details
 * @param addrLine1 - String being line 1 of the address
 * @param addrLine2 - String being line 2 of the address
 * @param city - String being the city in which the address is located
 * @param postcode - String being the postcode / zip code of the address
 */
public Address(String addrLine1, String addrLine2, String city, String postcode){
    this();
    this.addressLine1 = addrLine1;
    this.addressLine2 = addrLine2;
    this.city = city;
    this.postcode = postcode;
}

/**
 * Accessor method to retrieve the first line of the address
 * @return - String being the first line of the postal address
 */
public String getAddressLine1() {
    return addressLine1;
}

/**
 * Accessor method to set the first line of the address
 * @param addressLine1 - String being the first line of the postal address
 */
public void setAddressLine1(String addressLine1) {
    this.addressLine1 = addressLine1;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the second line of the address
 * @return - String being the second line of the postal address
 */
public String getAddressLine2() {
    return addressLine2;
}

/**
 * Accessor method to set the second line of the address
 * @param addressLine2 - String being the second line of the postal address
 */
public void setAddressLine2(String addressLine2) {
    this.addressLine2 = addressLine2;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the city from the address
 * @return - String being the city in which the address is located
 */
public String getCity() {
    return city;
}

/**
 * Accessor method to set the city in which the address is located
 * @param city - String being the city in which the address is located
 */
public void setCity(String city) {
    this.city = city;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the postcode of the address
 * @return - String being the postcode of the address
 */
public String getPostcode() {
    return postcode;
}

/**
 * Accessor method to set the postcode of the address
 * @param postcode - String being the postcode of the address
 */
public void setPostcode(String postcode) {
    this.postcode = postcode;
    this.subjectDelegate.notifyObservers();
}

@Override
public Boolean registerObserver(IObserver o) {
    return this.subjectDelegate.registerObserver(o);
}

@Override
public Boolean removeObserver(IObserver o) {
    return this.subjectDelegate.removeObserver(o);
}

@Override
public void notifyObservers() {
    this.subjectDelegate.notifyObservers();
}

@Override
public String toString() {
    //Using a StringBuilder it is more memory efficient than concatenating strings with "+"
    //This is because strings are "immutable objects" so building up a string using "+"
    //means allocating memory for a whole new object each time we add to the string.
    //StringBuilder uses a "mutable array of characters" allocating memory for 
    //only one object.
    StringBuilder resultBuilder = new StringBuilder();
    resultBuilder.append(this.addressLine1);
    resultBuilder.append(", ");
    if(null != this.addressLine2 && !this.addressLine2.isEmpty()){
        resultBuilder.append(this.addressLine2);
        resultBuilder.append(", ");
    }
    if(null != this.city && !this.city.isEmpty()){
        resultBuilder.append(this.city);
        resultBuilder.append(", ");
    }
    if(null != this.postcode && !this.postcode.isEmpty()){
        resultBuilder.append(this.postcode);
    }
    return resultBuilder.toString();
}

}我想你想要的是

        Address defaultColAddress = new Address(addrLine1, addrLine2, city, postcode);

地址是您创建的类吗?如果是这样的话,给我们看看它的代码我现在已经添加了完整的地址类谢谢你的帮助,我应该知道的。