0

Here is a constructor I wrote that holds address information, but I'm having trouble with the final part, where I have to return a string that holds name, address, city, state zipcode. What would be the correct way to write it?

public class Address {

private String name;
private String address;
private String state;
private String city;
private String zipcode;

public Address(String name, String address, String state, String city, String zipcode){

    this.name = name;
    this.address = address;
    this.state = state;
    this.city = city;
    this.zipcode = zipcode;


}

public Address(){

    name = "occupant";
    address = " ";
    state = " ";
    city = " ";
    zipcode = " ";


}

public void setAddress(String Address){

    this.address = Address;

}

public void setstate(String state){

    this.state= state;


}

public void setcity(String city){

    this.city = city;
}

public void setzipcode(String code){

   this.zipcode = code; 

}

public String getaddress(){  // Return string that contains name and address and city and zipcode

    return getaddress() + " " + return state + " " + return city + " " + return code;



}

}

1
  • Do you need it in one whole string? Or could it be an array of strings? Also why are you calling getaddress() inside getaddress()???? Commented Apr 4, 2012 at 21:51

3 Answers 3

3
 return address + " " + state + " " + city + " " + code;

A few notes:

  • only one return, followed by the object to return, which is a result of string concatenation
  • call your concatenating method getFullAddress() to distinguish it from the getter.
  • use lower-case variable names
  • getters and setters should get camel-case: setCity(), getState()
Sign up to request clarification or add additional context in comments.

2 Comments

I would also like to point out that you can override your address object's toString
yes, but since .toString() should be used mainly for debugging, if he needs this for other purposes, he'd better make a new method
1

I would rename the variable address to street for clarity. No matter though, rather than overridding toString() I would add getters on each of the fields and add a static utility class to print the address with the following method

static String formatAddress(Address address){
    final String formatter = "Address\n%s\n%s\n%s, %s  %s";
    return String.format(formatter, address.getName(),address.getAddress(),address.getCity(), address.getState(), address.getZipcode());
}

1 Comment

Obviously this is a simple formatter but it should get you on the path.
0

I think your problem is probably that you are calling getaddress() inside of getaddress() rather than using address

EDIT: and as Bozho pointed out, only one return per statement in a method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.