views:

117

answers:

7

I have a method in Eclipse as below.

public String toString() {
  return "HouseVo [ "
  + "Name  :  " + this.name == null ? "" : this.name
  + "Address  :  " + this.address == null ? "" : this.address;
}

When I format it becomes:

return "HouseVo [ " + "Name  :  " + this.name == null ? ""
        : this.name + "Address  :  " + this.address == null ? ""
               : this.address;

Any way to fix it so it correctly formats?

+5  A: 

The ternary operator has a very low precedence. The fact that Eclipse is restructuring your code is a hint that it doesn't do what you think it does. Try this:

public String toString() {
  return "HouseVo [ "
  + "Name  :  " + (this.name == null ? "" : this.name)
  + "Address  :  " + (this.address == null ? "" : this.address)
}
Marcelo Cantos
Yeah parenthesys help, another alternative : http://stackoverflow.com/questions/2833680/problem-formating-in-eclipse/2838015#2838015
javaguy
+1  A: 

Your could try configuring the formatter in the Eclipse preferences (Java > Code Style > Formatter) and edit the profiles.

There are a lot of options there regarding indentation, braces, new lines, line wrapping, white spaces, control statements etc.

Not sure if you can fix this exact formatting but in the line wrapping section you can make modifications for the Expressions > Conditionals option. See if some style there is OK with what you need.

dpb
+3  A: 

There's no one absolutely right way to automatically format code that Eclipse follows.

That said, I'd instead refactor the code to something like this:

static String emptyIfNull(String s) {
   return (s == null) ? "" : s;
}

public String toString() {
  return String.format(
     "HouseVo [ Name  :  %sAddress  :  %s",
     emptyIfNull(this.name),
     emptyIfNull(this.address)
  );
}

This uses String.format and it makes it obvious that currently, your toString() format does not have a closing ], and the Address field immediatelly follows the Name value without any delimiter in between.

Using a formatting string makes it easy to switch to, say, something like this:

     "HouseVo [ Name: %s, Address: %s ]"

So not only is the code more readable, but it's also easier to maintain.

See also

Related question

polygenelubricants
Indeed, this is much more readable. To make it even better, perhaps a clearer name could be found for the helper method? I suggest `emptyIfNull`.
meriton
A: 

When you use the format facility given in eclipse, it will format it like that only.

It is better to separate your string concatenation if you require in more readable format as shown below.

java.lang.StringBuffer sb = new java.lang.StringBuffer("HouseVo [ ");
sb.append("Name  :  " + (this.name == null ? "" : this.name));
sb.append("Address  :  " + (this.address == null ? "" : this.address));
return sb.toString();
RaviG
`StringBuilder` instead of `StringBuffer`.
polygenelubricants
A: 

Use //'s:

public String toString() {
  return "HouseVo [ " //
  + "Name  :  " + this.name == null ? "" : this.name //
  + "Address  :  " + this.address == null ? "" : this.address;
}

In this particular case though, I would extract each ?:-operator result to a local variable and then concat them at the end. Makes it easier to read.

Thorbjørn Ravn Andersen
+1  A: 

Putting some parenthesis might help. Try this:

public String toString() { 
   return "HouseVo [ " 
          + ("Name  :  " + this.name == null ? "" : this.name)
          + ("Address  :  " + this.address == null ? "" : this.address)
          + "]"; 
} 
javaguy
+1  A: 

You can create one xml file in which you can specify that how you want to format your code and then you can add that xml file using preferences-Java - Code Style - Formatter and inport that xml file in it.

Here is the sample code to write that xml file

Rupeshit
Where is the sample code?
fastcodejava
I think because of stack overflow restriction that code is not visible but I will put it on google and will send you its link soon
Rupeshit