Java Scanner String input example

Java’s Scanner String input method

To take String input from the user with Java’s Scanner class, just follow these steps

  1. Import java.util.*; to make Java’s Scanner class available
  2. Use the new keyword to create an instance of the Scanner class
  3. Pass the static System.in object to the Scanner’s Java constructor
  4. Use Scanner’s next() method to take input one String at a time
  5. Optionally use the Scanner’s hasNext() method to loop over the process

 String user input example

The following example uses the Java Scanner class to take String input from the user:

import java.util.Scanner;
public class ScannerUserInput {
  public static void main(String[] args) {
    // String input with the Java Scanner
    System.out.println("How old are you?");
    Scanner stringScanner = new Scanner(System.in);
    String age = stringScanner.next();
    System.out.println(age + " is a good age to be!");
  }
}

Console-based interaction with this example looks like this:

How old are you?
50
50 is a good age to be!

Scanner user input processing

Now imagine a user types in 20 30 40 50 at the console. What happens?

Here’s the result:

How old are you?
20 30 40 50
20 is a good age to be!

Notice how the output includes only the first String the Java Scanner read. The rest of the text is ignored.

This is because the Scanner class tokenizes the input String based on any whitespace between words.

Scanner hasNext example

To get each individual text String, we must create a loop and iterate through the input String with the Scanner’s hasNext() method:

System.out.println("How old are you?"); 
Scanner stringScanner = new Scanner(System.in); 
// Process each Java Scanner String input
while (stringScanner.hasNext()) {
  String age = stringScanner.next(); 
  System.out.println(age + " is a good age to be!");
}

If the user now types in 20 30 40 50, the program iterates through the text String four times, once for each number.

Here’s the output of the updated Scanner String input processing program:

How old are you?
20 30 40 50
20 is a good age to be!
30 is a good age to be!
40 is a good age to be!
50 is a good age to be!

Scanner next vs nextLine for user input

As we have seen, the Scanner’s next() method consumes a line of text, tokenizes the input based on any whitespace, and then iteratively loops through each individual item. The Scanner’s nextLine() method works a bit differently.

In contrast to next(), nextLine() does not tokenize input. It simply returns a complete line of text.

In this example, we change from the Scanner next() method to the nextLine() method and compare the results:

System.out.println("How old are you?"); 
Scanner stringScanner = new Scanner(System.in); 
// Process each Java Scanner String input
while (stringScanner.hasNext()) {
 String age = stringScanner.nextLine(); 
 System.out.println(age + " is a good age to be!");
}

How old are you?
20 30 40 50
20 30 40 50 is a good age to be!

Notice how the input is not split up, or tokenized, into separate, individual Strings?

The difference between the Java Scanner’s next() and nextLine() methods is that next() chunks a line of input into individual text Strings, while nextLine() returns an entire line of user input exactly the way it was sent to the Scanner.

Beyond Strings with Java’s Scanner class

Java’s Scanner String input methods represent only a small part of the class’s functionality. Methods exist to directly convert input into:

  • floats
  • doubles
  • ints
  • longs
  • BigDecimal objects
  • BigInteger objects

Interestingly, there is no method for Scanner char input, although there are strategies that can be employed to make that work.

But if your program needs to take command-line input from the user in the form of a String, Java’s Scanner class provides all of the methods you will need.

Java user input and output strategies

Here’s how to get input into your Java apps and format the output.