0

How do I break a long line of code like this into several lines in Java?

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy tex")
Margo Eastham
  • 605
  • 1
  • 10
  • 20

7 Answers7

3

have you tried this?

System.out.println("Lorem Ipsum is simply dummy text"
                   + " of the printing and typesetting industry. "
                   + "Lorem Ipsum has been the industry's "
                   + "standard dummy tex");
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Nazgul
  • 1,892
  • 1
  • 11
  • 15
1

You could do

System.out.println("Lorem Ipsum is simply dummy text of the printing" +  
                    "and typesetting industry. Lorem Ipsum has been " + 
                    "the industry's standard dummy tex");

The resultant String will be the same as the original at the bytecode level

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

use String concatination, like

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry." +
        " Lorem Ipsum has been the industry's standard dummy tex");
jmj
  • 237,923
  • 42
  • 401
  • 438
1

Try something like:

System.out.println("Lorem Ipsum is simply dummy text of the printing and" + 
" typesetting industry. Lorem Ipsum has been the industry's" +
" standard dummy tex")

Use "+" to concatenate two string which lives in two different lines.

SMA
  • 36,381
  • 8
  • 49
  • 73
1

You can also apply line breaks- and either do it with more than a single command, like Rei did - but with a print on each part and the final one will be println, or use the \n special character:

System.out.println("Lorem Ipsum is simply dummy text of the printing\n" +  
                "and typesetting industry. Lorem Ipsum has been\n " + 
                "the industry's standard dummy tex");
A. Abramov
  • 1,823
  • 17
  • 45
1

Try this:System.out.println("Lorem Ipsum is simply dummy text of the "+ "printing and typesetting industry. Lorem Ipsum has been the industry's "+ "standard dummy tex");

Dhruv Ramani
  • 2,633
  • 2
  • 22
  • 29
1

If you have String as :

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy tex");

Than you can do it this vay :

System.out.println("Lorem Ipsum is simply dummy text of the printing and typesetting industry."  
                  +"Lorem Ipsum has been the industry's standard dummy tex");

Or you can split your String: Add any character where you want to split String or use some char that is already in String.

String string1 = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.-Lorem Ipsum has been the industry's standard dummy tex";

String[] parts = string1.split("-");
String part0 = parts[0]; // Lorem Ipsum is simply dummy text of the printing and typesetting industry.
String part1 = parts[1]; //Lorem Ipsum has been the industry's standard dummy tex
System.out.println(part0);

output: Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Note : if you want to split by "." , it stands for every character so

instead of

split(".");

use

split("\\.");

or

split(Pattern.quote("."));

To test beforehand if the string contains a -, just use

String#contains()

if (string.contains("-")) {
    // Split it. 
} 
else {
  throw new IllegalArgumentException("String " + string1 + " does not contain-"); 
 }
RobertS
  • 135
  • 1
  • 11