-1

For 'case 1' below, the console prints the text as intended.

case 1: System.out.println ("Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text "); break;

However, I want to format the code so that, when reading the code, the second line of text is aligned with the first: so that the S in 'System' is aligned with the T of the 'Text' that is at the start of the second line of text. If I use the tab to indent the second line, the tab appears in the text that is printed to the console. Is there a way around this?

danger mouse
  • 1,457
  • 1
  • 18
  • 31
  • 1
    You have a literal string which cannot be formatted. As @MuhammetAliAsan alluded to, you would have to break up the literal string and then your IDE (e.g. eclipse) will do the proper formatting for you. – Jared Dec 02 '14 at 06:18

4 Answers4

1

A stupid workaround:

  System.out.println("Text text text " +
  "text text text");
Olavi Mustanoja
  • 2,045
  • 2
  • 23
  • 34
1

Java has a "+" operator for strings which will concat multiple strings together. However, String concat using "+" is known to cause performance issues. Hence, it is better to save the entire string as a single string instead of splitting for readability.

case 1:
      System.out.println ("Text Text Text Text" +
                         " Text Text Text Text" +
                         " Text Text Text Text Text ");
      break;

If you are very particular, then you can consider a Stringbuffer or stringbuilder which has better performance than "+".

Using stringbuffer, you can do something like,

case 1:
      System.out.println ( new StringBuffer
                                 (" Text Text Text Text")  
                         .append (" Text Text Text Text")  
                         .append (" Text Text Text Text")  );
      break;
Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
0

Just use Concatination (+) of Java String. See Example

case 1: System.out.println ("Text Text Text Text Text Text Text Text Text Text Text" +
        " Text Text Text Text Text Text Text Text Text Text "); break;
Secondo
  • 451
  • 4
  • 9
0

You can simply split the output string into multiple parts with the "+" operand like so

You need to add more quotations before and after the +: " " + " " + " " you can do this for as long as you like, this is also how you easily put variables inside an output string

System.out.println("Text Text Text Text Text Text Text Text Text Text Text Text" +
"Text Text Text Text Text Text Text Text Text Text"); break;

System.out.println("something " + variable1 + " something else " + variable2);
David Coler
  • 453
  • 3
  • 8