[java] 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?

This question is related to java printing new-operator println

The answer is


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

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")

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


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.


/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")
    

It does create a new line. Try:

System.out.println("---\n###");

//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" + "----------------------------");

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.


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

\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.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to printing

How do I print colored output with Python 3? Print a div content using Jquery Python 3 print without parenthesis How to find integer array size in java Differences Between vbLf, vbCrLf & vbCr Constants Printing variables in Python 3.4 Show DataFrame as table in iPython Notebook Removing display of row names from data frame Javascript window.print() in chrome, closing new window or tab instead of cancelling print leaves javascript blocked in parent window Print a div using javascript in angularJS single page application

Examples related to new-operator

Java FileOutputStream Create File if not exists How to add to an existing hash in Ruby Expression must have class type Why should C++ programmers minimize use of 'new'? Creating an object: with or without `new` int *array = new int[n]; what is this function actually doing? Open button in new window? How to open in default browser in C# Print in new line, java Deleting an object in C++

Examples related to println

How to print multiple variable lines in Java What is the equivalent of Java's System.out.println() in Javascript? Division of integers in Java Print in new line, java difference between System.out.println() and System.err.println()