Print in new line, java

Upvote picture Upvote · Oct 24, 2010 · Viewed 462k times · Source

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?

Answer

Jigar Joshi picture Jigar Joshi · Oct 24, 2010
    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

    System.out.println("line 1" + newLine + "line2");