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?

7 Answers 7

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah parenthesys help, another alternative : stackoverflow.com/questions/2833680/…
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

1 Comment

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.
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.

Comments

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

1 Comment

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

1 Comment

StringBuilder instead of StringBuffer.
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.

Comments

-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)
          + "]"; 
} 

1 Comment

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.

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.