0

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?

Deduplicator
  • 41,806
  • 6
  • 61
  • 104
fastcodejava
  • 35,219
  • 24
  • 124
  • 181

7 Answers7

6

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
  • 167,268
  • 37
  • 309
  • 353
  • Yeah parenthesys help, another alternative : http://stackoverflow.com/questions/2833680/problem-formating-in-eclipse/2838015#2838015 – javaguy May 14 '10 at 22:35
3

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

Community
  • 1
  • 1
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    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 Jun 05 '10 at 13:10
1

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.

1

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
  • 1,428
  • 1
  • 12
  • 23
  • 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 Jun 06 '10 at 08:53
0

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();
Ravindra Gullapalli
  • 8,591
  • 3
  • 41
  • 66
0

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
  • 68,906
  • 28
  • 171
  • 323
-1

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
  • 4,076
  • 10
  • 30
  • 35
  • 1
    This doesn't do what OP intended either. The + between "Name" and `this.name` will take precedence over the ternary expression. @Marcelo's answer above is correct. – Cheezmeister Oct 14 '12 at 03:31