Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print in new line, java

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?

like image 566
UpCat Avatar asked Oct 24 '10 12:10

UpCat


People also ask

Does printf add a new line Java?

Each format specifier says to output the next item in the argument list at that place in the format. The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies.

What are \n in Java?

\n is an escape character for strings that is replaced with the new line object. Writing \n in a string that prints out will print out a new line instead of the \n. Java Escape Characters.

What does \t and \n Do Java?

\t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point.


1 Answers

    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.      System.out.println("line 1" + newLine + "line2"); 
like image 97
jmj Avatar answered Sep 30 '22 12:09

jmj