How to take a string with blank spaces as input in Java

How to take a string with blank spaces as input in Java:

In this post, we will learn how to take a string with blank spaces as input in Java. We will use the next() and nextLine() methods to read the strings with blank spaces.

next() and nextLine() methods of the Scanner class:

The next() and nextLine() methods of the Scanner class can be used to read user inputs from the console. We can use any of these methods to read the user input.

But there is a difference between the next() and nextLine() method. The next() method can read strings up to the space. For example, if you use the next() method to read the string Hello World, it will read only the Hello word. It will stop if it finds any blank space.

But, we can use the nextLine() method to read a string with blank spaces. It will read the whole Hello World string. The nextLine() method stops if it reads a newline character \n or if the user press the enter key.

So, if you want to take a string with blank spaces as input from the console, you have to use the newLine() method.

Example of newLine:

Let’s take a look at the below example:

import java.util.Scanner;

public class Example1 {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            String str;

            System.out.println("Enter a string: ");
            str = sc.nextLine();

            System.out.println("You have entered: " + str);
        }
    }
}

Download it on Github

In this program,

  • We have created one Scanner object sc to read the user input.
  • It asks the user to enter a string.
  • It uses the nextLine() method to read the user input string and assigns it to the str variable.
  • The last line is printing the str variable.

If you run this program, it will print output as below:

Enter a string: 
Hello World
You have entered: Hello World

Java read strings with spaces

Example with next():

Let’s try with the next() method. If you use this method, it will not read the blank spaces.

import java.util.Scanner;

public class Example2 {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            String str;

            System.out.println("Enter a string: ");
            str = sc.next();

            System.out.println("You have entered: " + str);
        }
    }
}

Download it on Github

If you run this program, it will print:

Enter a string: 
Hello World
You have entered: Hello

How to use the next() method to read a string with blank spaces:

We can also use the next() method to read a string with blank spaces. We can set the scanner’s delimiting pattern with the useDelimiter method. It takes the pattern as the parameter. If we pass "\n" as the parameter, it will read the string up to the new line.

For example:

import java.util.Scanner;

public class Example3 {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in).useDelimiter("\n")) {
            String str;

            System.out.println("Enter a string: ");
            str = sc.next();

            System.out.println("You have entered: " + str);
        }
    }
}

Download it on Github

If you run this program, it will print output similar to the example of nextLine():

Enter a string: 
Hello World
You have entered: Hello World

You might also like: