54

I have following code :

    System.out.println(" | 1  2  3  4  5  6  7  8  9");
    System.out.println("----------------------------");
    System.out.println("");

I use println to create a new line. Is it possible to do the same using \n or \r? I tried to add \n to the second println statment and continue printing with the print method but \n does not create a new line.

any ideas?

DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601

11 Answers11

89
    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

    System.out.println("line 1" + newLine + "line2");
jmj
  • 237,923
  • 42
  • 401
  • 438
34
System.out.println("hello"+"\n"+"world");
Pang
  • 9,564
  • 146
  • 81
  • 122
13

Your best shot would be with

String.format("%n")

or

System.out.printf("%n");

It is supposed to print a newline character, depending on the current platform, so it's perfect for the console.

If you are printing to a file, then it depends.

Pang
  • 9,564
  • 146
  • 81
  • 122
vstoyanov
  • 871
  • 5
  • 14
  • This is far by the best answer since it address the problems discussed: 1. Platform and 2. Java Versions. – tortal Feb 19 '21 at 23:38
9

You should use the built in line separator. The advantage is that you don't have to concern what system you code is running on, it will just work.

Since Java 1.7

System.lineSeparator()

Pre Java 1.7

System.getProperty("line.separator")
Shawn Vader
  • 12,285
  • 11
  • 52
  • 61
9

It does create a new line. Try:

System.out.println("---\n###");
biasedbit
  • 2,860
  • 4
  • 30
  • 47
  • 1
    In the past, the Apple Mac requirse lines to be separated by '\r', So its better to write system independent code , check my solution – jmj Oct 24 '10 at 12:46
  • He's talking about "new line". Had he requested "new line + carriage return" I'd pointed him to \r\n :) If we're going to be picky, then I'd suggest using a StringBuilder rather than concatenating strings. – biasedbit Oct 24 '10 at 12:54
  • 2
    For `"line 1" + newLine + "line2"` using a StringBuilder explicitly would be counter-productive. The compiler can optimize this by itself. – Thilo Oct 24 '10 at 12:58
7

You might try adding \r\n instead of just \n. Depending on your operating system and how you are viewing the output, it might matter.

James Branigan
  • 1,164
  • 7
  • 9
5

\n creates a new line in Java. Don't use spaces before or after \n.

Example: printing It creates\na new line outputs

It creates
a new line.

Pops
  • 30,199
  • 37
  • 136
  • 151
Salahuddin
  • 51
  • 1
  • 1
1

Since you are on Windows, instead of \n use \r\n (carriage return + line feed).

tushar
  • 3
  • 3
0
//Case1:
System.out.println(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");

//Case2:
System.out.printf(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");

//Case3:
System.out.print(" 1  2  3  4  5  6  7  8  9" + "\n" + "----------------------------");
Bhavya Jain
  • 591
  • 6
  • 15
-2

"\n" this is the simple method to separate the continuous String

Alice
  • 3,958
  • 2
  • 24
  • 28
Santhosh Raja
  • 39
  • 1
  • 9
-2
System.out.print(values[i] + " ");
//in one number be printed
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • number will be printed in one line like 1 2 3 4 5 for new line we can use \n – Gajendra kumar Mar 06 '17 at 13:11
  • Does not answer the question. Even worse - attempts to solve a problem unrelated making it only confusing. Moreover - there is no explanation at all! – tortal Feb 19 '21 at 23:44