Skip to content Skip to sidebar Skip to footer

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

jmj's user avatar

4

  • 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

              System.out.println("hello"+"\n"+"world");                          

Pang's user avatar

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's user avatar

Pang

9,223 146 gold badges 85 silver badges 118 bronze badges

answered Oct 24, 2010 at 12:44

vstoyanov's user avatar

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

biasedbit's user avatar

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 Vader's user avatar

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

James Branigan's user avatar

\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's user avatar

Pops

29.6k 36 gold badges 133 silver badges 151 bronze badges

answered Sep 23, 2011 at 7:29

Salahuddin's user avatar

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

tushar's user avatar

              //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 Jain's user avatar

/n and /r usage depends on the platform (Window, Mac, Linux) which you are using.
But there are some platform independent separators too:

  1.                     System.lineSeparator()                                      
  2.                     System.getProperty("line.separator")                                      

β.εηοιτ.βε's user avatar

β.εηοιτ.βε

27.5k 11 gold badges 56 silver badges 69 bronze badges

answered Jun 28, 2020 at 18:27

Sahil Bhandari's user avatar

1

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

Alice's user avatar

Alice

3,908 2 gold badges 23 silver badges 28 bronze badges

answered Dec 12, 2014 at 6:40

Santhosh Raja's user avatar

0

                System.out.print(values[i] + " "); //in one number be printed                              

Arsen Mkrtchyan's user avatar

answered Mar 6, 2017 at 13:10

Gajendra kumar's user avatar

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

mayoyounfat.blogspot.com

Source: https://stackoverflow.com/questions/4008223/print-in-new-line-java

Post a Comment for "How to Continue a System out on the Next Line Java"