How to Continue a System out on the Next Line Java
12 Answers 12
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS. System.out.println("line 1" + newLine + "line2"); answered Oct 24, 2010 at 12:45
jmjjmj
234k 42 gold badges 394 silver badges 433 bronze badges
4
System.out.println("hello"+"\n"+"world");
Pang
9,223 146 gold badges 85 silver badges 118 bronze badges
answered Dec 29, 2014 at 15:47
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,223 146 gold badges 85 silver badges 118 bronze badges
answered Oct 24, 2010 at 12:44
vstoyanovvstoyanov
861 5 silver badges 14 bronze badges
1
-
This is far by the best answer since it address the problems discussed: 1. Platform and 2. Java Versions.
Feb 19, 2021 at 23:38
It does create a new line. Try:
System.out.println("---\n###"); answered Oct 24, 2010 at 12:41
biasedbitbiasedbit
2,810 4 gold badges 29 silver badges 40 bronze badges
3
-
In the past, the Apple Mac requirse lines to be separated by '\r', So its better to write system independent code , check my solution
Oct 24, 2010 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.
Oct 24, 2010 at 12:54
-
For
"line 1" + newLine + "line2"using a StringBuilder explicitly would be counter-productive. The compiler can optimize this by itself.Oct 24, 2010 at 12:58
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") answered Jan 30, 2014 at 17:13
Shawn VaderShawn Vader
12.2k 11 gold badges 52 silver badges 60 bronze badges
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.
answered Oct 24, 2010 at 12:42
\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
29.6k 36 gold badges 133 silver badges 151 bronze badges
answered Sep 23, 2011 at 7:29
SalahuddinSalahuddin
51 1 silver badge 1 bronze badge
1
-
where does it say that using \n in java will use the correct newline, such as \r\n on Windows?
Nov 11, 2016 at 23:31
Since you are on Windows, instead of \n use \r\n (carriage return + line feed).
answered Aug 28, 2013 at 9:03
//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" + "----------------------------"); answered Jul 14, 2016 at 18:15
Bhavya JainBhavya Jain
571 6 silver badges 15 bronze badges
/n and /r usage depends on the platform (Window, Mac, Linux) which you are using.
But there are some platform independent separators too:
-
System.lineSeparator() -
System.getProperty("line.separator")
β.εηοιτ.βε
27.5k 11 gold badges 56 silver badges 69 bronze badges
answered Jun 28, 2020 at 18:27
1
"\n" this is the simple method to separate the continuous String
Alice
3,908 2 gold badges 23 silver badges 28 bronze badges
answered Dec 12, 2014 at 6:40
0
System.out.print(values[i] + " "); //in one number be printed
answered Mar 6, 2017 at 13:10
2
-
number will be printed in one line like 1 2 3 4 5 for new line we can use \n
Mar 6, 2017 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!
Feb 19, 2021 at 23:44
Source: https://stackoverflow.com/questions/4008223/print-in-new-line-java
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.Aug 14, 2014 at 0:03
it does except that it adds line separator at the end
Aug 14, 2014 at 0:07
Great! This is really useful for me.
Jul 22, 2015 at 7:47