10

I have the following message:

System.out.println("Players take turns marking a square. Only squares not already marked can be picked. Once a player has marked three squares in a row, he or she wins! If all squares are marked and no three squares are the same, a tied game is declared. Have Fun!");

This is a very long message and streaks across my screen, I want to break it up into segments so it does not break the design flow of my code. When I press enter though, Java no longer interprets the line as a string; Java believes it is a separate statement. How can I make Java interpret multiline print statements?

1
  • And also use StringBuilder to construct this message 'cause it would streak across the source viewer (IDE etc.) sceen as well. Commented Dec 10, 2014 at 19:08

6 Answers 6

19

Are you looking for something like this?

String line = "Information and stuff" + 
          "More information and stuff " + 
          "Even more information and stuff";

System.out.println(line);
Sign up to request clarification or add additional context in comments.

1 Comment

Is there some kind of generator online that will print out multi-line code samples as as String and print them with proper indenting, etc.? Update: It looks like IntelliJ will preserve the formatting with a simple copy-paste.
7

Following Java's coding conventions:

System.out.println("Players take turns marking a square. "
                   + "Only squares not already marked can be picked. "
                   + "Once a player has marked three squares in a row, "
                   + "he or she wins! If all squares are marked and no "
                   + "three squares are the same, a tied game is declared. "
                   + "Have Fun!");

Always break with the concatenation operator to start the next line for readability.

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

Comments

2

You can type: System.out.println("This is the first line \n" + "This is the second line")

Comments

1

System.out.println("Players take turns marking a square."
+ "\nOnly squares not already marked can be picked."
+ "\nOnce a player has marked three squares in a row, he or she wins!"
+ "\nIf all squares are marked and no three squares are the same, a tied game is declared."
+ "\nHave Fun!");

Done.

Comments

0

add '\n' where ever you want a new line.

1 Comment

geez. i apologize. read it in a rush and replied quickly. my bad.
0

Just use triple quote, and you can have as may line as you want:

System.out.println("""
            line 1
            line 2
            line 3
            """);

Comments

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.