Adding a Newline Character to a String

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to add a new line character to a string in Java. We will discuss different ways to add a new line, and explore the platform-dependent and platform-independent approaches for adding new lines. We will also learn about Java's line separator method and platform-independent newline character.

Using \n and \r characters

In this step, we will learn how to use the platform-dependent newline characters \n and \r to add a new line to a string.

Add the following code to the NewLineCharacter.java file:

public class NewLineCharacter {
  public static void main(String args[]) {
    String str1 = "Java is awesome";
    String str2 = "Java\nis\nawesome";
    String str3 = "Java\ris\rawesome";
    System.out.print(str1 + "\n" + str2 + "\n" + str3);
  }
}

In the code above, we have defined three strings: str1, str2, and str3. In str2, we have used the \n character to add a new line. In str3, we have used the \r character to add a new line.

Compile and run the program in the terminal:

javac NewLineCharacter.java && java NewLineCharacter

The program will output:

Java is awesome
Java
is
awesome
Java
is
awesome

Using platform-independent line separator

In this step, we will use the platform-independent System.lineSeparator() method to add a newline character.

Add the following code to the NewLineCharacter.java file:

public class NewLineCharacter {
  public static void main(String args[]) {
    String str1 = "Java is awesome";
    String newline = System.lineSeparator();
    String str2 = "Java" + newline + "is" + newline + "awesome";
    System.out.print(str1 + "\n" + str2);
  }
}

In the code above, we have used the System.lineSeparator() method to retrieve the platform-independent line separator. We have concatenated newline with the string Java to create a new line.

Compile and run the program in the terminal:

javac NewLineCharacter.java && java NewLineCharacter

The program will output:

Java is awesome
Java
is
awesome

Using platform-independent newline character

In this step, we will use the platform-independent newline character %n to add a new line.

Add the following code to the NewLineCharacter.java file:

public class NewLineCharacter {
  public static void main(String args[]) {
    String str1 = "Java is awesome";
    String str2 = "Java%nis%nawesome";
    System.out.printf(str1 + "%n" + str2);
  }
}

In the code above, we have used the %n character to add a new line. We have used the printf() method to print the strings to the console.

Compile and run the program in the terminal:

javac NewLineCharacter.java && java NewLineCharacter

The program will output:

Java is awesome
Java
is
awesome

Using System.out.println() method

In this step, we will use the System.out.println() method to add a new line.

Add the following code to the NewLineCharacter.java file:

public class NewLineCharacter {
  public static void main(String args[]) {
    String str1 = "Java";
    String str2 = "is ";
    String str3 = "awesome";
    System.out.println(str1);
    System.out.println(str2);
    System.out.print(str3); //Not using println() as we do not need a new line after this
  }
}

In the code above, we have used the println() method to add a new line between the strings.

Compile and run the program in the terminal:

javac NewLineCharacter.java && java NewLineCharacter

The program will output:

Java
is
awesome

Summary

In this lab, we have learned how to add a new line character to a string in Java. We explored different ways to add a newline and learned about the platform-dependent and platform-independent approaches for adding new lines. We also learned about Java's line separator method and platform-independent newline character.

Other Java Tutorials you may like