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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/format("`Format`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/format -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/classes_objects -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/class_methods -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/modifiers -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/oop -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/identifier -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/comments -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/data_types -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/operators -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/output -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/strings -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/variables -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/string_methods -.-> lab-117387{{"`Adding a Newline Character to a String`"}} java/system_methods -.-> lab-117387{{"`Adding a Newline Character to a String`"}} end

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