Print in new line, java
Asked Answered
D

12

58

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?

Dhar answered 24/10, 2010 at 12:38 Comment(2)
Call System.out.println(); repeatedly :PKrakau
I wonder why you have problems to simply write System.out.println("\n)"; The "\n" considers as escape sequence for C/C++/C# and Java aswell.Pacificism
A
90
    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

    System.out.println("line 1" + newLine + "line2");
Apoplexy answered 24/10, 2010 at 12:45 Comment(4)
Since Java 7 you have System.lineSeparator() docs.oracle.com/javase/7/docs/api/java/lang/…Huss
is System.out.println(); looked down upon? Doesn't this do the same thing? This is my second day in Java, came to find best practices.Dosage
it does except that it adds line separator at the endApoplexy
Great! This is really useful for me.Abott
S
35
System.out.println("hello"+"\n"+"world");
Skate answered 29/12, 2014 at 15:47 Comment(0)
G
14

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.

Grandmother answered 24/10, 2010 at 12:44 Comment(1)
This is far by the best answer since it address the problems discussed: 1. Platform and 2. Java Versions.Eurythmic
A
9

It does create a new line. Try:

System.out.println("---\n###");
Anhydrous answered 24/10, 2010 at 12:41 Comment(3)
In the past, the Apple Mac requirse lines to be separated by '\r', So its better to write system independent code , check my solutionApoplexy
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.Anhydrous
For "line 1" + newLine + "line2" using a StringBuilder explicitly would be counter-productive. The compiler can optimize this by itself.Westberry
P
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")
Pieter answered 30/1, 2014 at 17:13 Comment(0)
F
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.

Floppy answered 24/10, 2010 at 12:42 Comment(0)
T
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.

Thecla answered 23/9, 2011 at 7:29 Comment(1)
where does it say that using \n in java will use the correct newline, such as \r\n on Windows?Mendacious
H
1

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

Hedwighedwiga answered 28/8, 2013 at 9:3 Comment(0)
N
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" + "----------------------------");
Neoimpressionism answered 14/7, 2016 at 18:15 Comment(0)
T
0

Calling System.out.println(); without any parameter terminates the current line by writing the line separator string.

Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n') (1).

  1. https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println--
Trice answered 11/11, 2023 at 17:9 Comment(0)
B
-2

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

Brook answered 12/12, 2014 at 6:40 Comment(0)
C
-2
System.out.print(values[i] + " ");
//in one number be printed
Clymer answered 6/3, 2017 at 13:10 Comment(2)
number will be printed in one line like 1 2 3 4 5 for new line we can use \nClymer
Does not answer the question. Even worse - attempts to solve a problem unrelated making it only confusing. Moreover - there is no explanation at all!Eurythmic

© 2022 - 2024 — McMap. All rights reserved.