开发者

Problem formatting in Eclipse

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?


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)
}


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

  • Formatting string syntax

Related question

  • Formatted printing in Java
    • Using the width and flags for left/right justification


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.


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


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();


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.


Putting some parenthesis might help. Try this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜