-1

I am trying to create a toString method which return the name, address and phone number of a person. The output format that I want as following:

Anna Brown

1234 Sweet Road

Los Angeles, CA 99999

Phone: 888.8888.888

The address is 1234 Sweet Road Los Angeles, CA 99999 but I want the city is in a new line. I tried this but it does not work. can someone help me out please?

public String toString()
{

  return String.format( "%-25s\n %-14s\n\n\n%-25s",
                            name,address, phone );
}
brownEyes
  • 31
  • 1
  • 5
  • 8
    Please expand on "does not work", so that we can better understand your code and your problem. – Hovercraft Full Of Eels Jul 29 '14 at 20:44
  • 2
    You probably should split the city from your address. – user1071777 Jul 29 '14 at 20:45
  • Also, depending on what you are using to view the output, you may need carriage return characters (\r) – SyntaxTerror Jul 29 '14 at 20:48
  • @Insertusernamehere or just use [%n](http://stackoverflow.com/questions/1883345/whats-up-with-javas-n-in-printf). – sgbj Jul 29 '14 at 20:50
  • How does your result should look exactly? I suspect you wanted to achieve something shown in this version of your question http://stackoverflow.com/revisions/25024635/2 but now you changed it to something different (each line is separated by empty line) so I am confused. – Pshemo Jul 29 '14 at 20:59
  • 1
    -1 Never just say "It does not work". *Always* describe what didn't work, what you expected, what you got, and copy/paste into your question any messages or output values that apply to the issue. – Hot Licks Jul 29 '14 at 20:59
  • So many upvotes of [HovercraftFullOfEels' comment](http://stackoverflow.com/questions/25024635/new-line-using-tostring-in-java#comment38917224_25024635) but so little votes to put this question on-hold as "unclear what you're asking". – Pshemo Jul 29 '14 at 22:26

2 Answers2

2

It appears that address has both the street address and the city. You'll want to put them in separate variables if you want them to be treated differently. Otherwise, address has to contain the \n between the street and city.

Jason Thompson
  • 4,643
  • 5
  • 50
  • 74
0

I think you need to separate your fields (as below adding address2) or put a '\n' in your address line,

static class Address {
  Address(String name, String address, String address2, String phone) {
    this.name = name;
    this.address = address;
    this.address2 = address2;
    this.phone = phone;
  }

  String name;
  String address;
  String address2;
  String phone;

  public String toString() {
    return String.format("%s%n%s%n%s\nPhone: %s",
        name, address, address2, phone);
  }
}

public static void main(String[] args) {
  String name="Anna Brown";
  String address="1234 Sweet Road";
  String address2="Los Angeles, CA 99999";
  String phone="888.8888.888";
  System.out.print(new Address(name, address, address2, phone));
}

Outputs

Anna Brown
1234 Sweet Road
Los Angeles, CA 99999
Phone: 888.8888.888

The above approach will write the correct line endings regardless of platform.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Hi Elliott, I should have mentioned that the user will enter the information about name, address, and phone. I followed your code but turned out like this: Enter customer name: anna brown Enter customer address: 1234 Sweet Road\nLos Angeles, CA 99999 Enter customer phone: 888.8888.888 anna brown 1234 Sweet Road\nLos Angeles, CA 99999 Phone: 888.8888.888 – brownEyes Jul 29 '14 at 21:02
  • @brownEyes If the user is entering the address as a single string then you'll have to parse out the street address from the city, state, and zip, which is impossible in the general case if it was a foreign address. Better to ask the user to enter them separately. – David Conrad Jul 29 '14 at 22:00