how to print in a new line in java using print

https://w‮‬ww.lautturi.com
how to print in a new line in java using print

To print a new line in Java using the print method, you can use the println method of the PrintStream class or the System.lineSeparator constant.

Here is an example of how you can print a new line in Java using the println method:

public class NewLinePrinter {
    public static void main(String[] args) {
        System.out.print("This is the first line.");
        System.out.println();
        System.out.print("This is the second line.");
    }
}

In this example, the main method uses the print method to print a string and then calls the println method to print a new line. The println method prints the argument followed by a new line, so it can be used to print a new line after printing a string with the print method.

This code will print the following output:

This is the first line.
This is the second line.

Here is an example of how you can print a new line in Java using the System.lineSeparator constant:

public class NewLinePrinter {
    public static void main(String[] args) {
        System.out.print("This is the first line.");
        System.out.print(System.lineSeparator());
        System.out.print("This is the second line.");
    }
}

In this example, the main method uses the print method to print a string and then prints the System.lineSeparator constant to print a new line. The System.lineSeparator constant represents the line separator of the current operating system, so it can be used to print a new line that is compatible with the platform.

This code will print the same output as the previous example.

Keep in mind that these are just two ways to print a new line in Java using the print method. You can use different techniques and data structures to achieve the same result, such as using the System.getProperty method to get the line separator from the system properties or using a string literal with the line separator character.

Created Time:2017-11-01 20:42:59  Author:lautturi