Break up a string literal over multiple lines
Asked Answered
M

2

6

Is there a way to break up a line of code so that it is read as continuous despite being on a new line in java?

public String toString() {

  return String.format("BankAccount[owner: %s, balance: %2$.2f,\
    interest rate: %3$.2f,", myCustomerName, myAccountBalance, myIntrestRate);
  }

The code above when I do this all on one line everything works dandy but when I try to do this on multiple lines it doesn't work.

In python I know you use a \ to start typing on a new line but print as one line when executed.

A Example in Python to clarify. In python this will print on one line using a backslash or ():

print('Oh, youre sure to do that, said the Cat,\
 if you only walk long enough.')

the User would see this as:

Oh, youre sure to do that, said the Cat, if you only walk long enough.

Is there similar ways to do this in java?? Thank you!

Middlebrow answered 20/4, 2017 at 0:10 Comment(8)
Nope, no way to do that in Java. The best you can do is concatenate with + across a line.Stud
Could you still String.format() it or would you have to do that for every line?Middlebrow
if you need a new line concat the String with /n in the end.Ideate
You can't have multi-line string literals. You can still String.format if the format is concatenated between multiple lines.Stud
@Louis Wasserman you get a single String literal in the class file as a result of a String constant expression. That's effectively a multi-line String literal, wouldn't you agree?Gilda
@Rajith Pemabandu, "concat the String with /n" will not embed a newline in the String. That advice is incorrect.Gilda
@LewBloch, no, I wouldn't agree. That term usually refers to how it appears in the source as it does in Python, not that the string contains newlines.Stud
I didn't say anything about a string containing newlines, that was the other guy.Gilda
M
9

Break up the string on the new line using + operator works.

public String toString() {
    return String.format("BankAccount[owner: %s, balance: "
            + "%2$.2f, interest rate:"
            + " %3$.2f]", 
            myCustomerName, 
            myAccountBalance, myIntrestRate);
}

Sample Output: BankAccount[owner: TestUser, balance: 100.57, interest rate: 12.50]

Marciamarciano answered 20/4, 2017 at 0:28 Comment(1)
Because the multi-line string addition involving only String literals and constants is stored as a single literal in the class file, it accomplishes precisely what's being asked for. Great answer.Gilda
L
0

Following Java's coding conventions:

public String toString() 
{
    return String.format("BankAccount[owner: %s, balance: %2$.2f",
                         + "interest rate: %3$.2f", 
                         myCustomerName, 
                         myAccountBalance, 
                         myIntrestRate);
}

Always have the concatenation operator at the beginning of the new line for readability.

https://www.oracle.com/technetwork/java/javase/documentation/codeconventions-136091.html#248

Hope this helps!

Brady

Lobbyism answered 21/4, 2019 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.