Print in new line, java

六眼飞鱼酱① 提交于 2019-11-27 11:40:01
    String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.

    System.out.println("line 1" + newLine + "line2");
System.out.println("hello"+"\n"+"world");
vstoyanov

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.

It does create a new line. Try:

System.out.println("---\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.

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

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

//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" + "----------------------------");
Gajendra kumar
System.out.print(values[i] + " ");
//in one number be printed
Santhosh Raja

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!