Get Input from a User in Java

When programming in Java or any other language, you will most likely need to use input information from a user. Java provides many different methods for getting in user information, but the most common and perhaps easiest to implement method is to use the Scanner object.

Steps

Videos

  1. Import the Scanner class. You can either choose to import the java.util.Scanner class or the entire java.util package. To import a class or a package, add one of the following lines to the very beginning of your code:
  2. Initialize a new Scanner object by passing the System.in input stream to the constructor. System.in is the standard input stream that is already open and ready to supply input data. Typically this stream corresponds to keyboard input.
  3. You can now read in different kinds of input data that the user enters. The Scanner class supports getting primitives such as int, byte, short, long in addition to getting strings. Here are some methods that are available through the Scanner class:
    • Read a byte - nextByte()
    • Read a short - nextShort()
    • Read an int - nextInt()
    • Read a long - nextLong()
    • Read a float - nextFloat()
    • Read a double - nextDouble()
    • Read a boolean - nextBoolean()
    • Read a complete line - nextLine()
    • Read a word - next()
    Here is an example of a program that uses different methods of the Scanner class to get different types of input:

Handling Exceptions

  1. Handle input exceptions. An InputMismatchException is thrown when the user enters data that doesn't match with the requested type. For example, if the user enters a String when an int is asked for, the program will throw an InputMismatchException and exit. There are several ways to handle this exception and resolve this problem so that your program can be foolproof.
  2. One way is to use a try-catch block to handle the InputMismatchException.
    • Note that we have to import java.util.InputMismatchException in order to use the InputMismatchException class.
    • We are using a while loop to ask the user the same question until the user enters the correct input.
    • Adding userInputScanner.nextLine(); in the catch part of the try-catch ensures that the Scanner acknowledges the "enter" key press from the user and functions as a way to clear the input buffer.
  3. Another way to make the user input foolproof is to only take in next lines from the Scanner. This way, we can ensure that everything that the Scanner returns is a String object and won't create any exceptions. Then, to convert the strings to integers or doubles, we can use the Integer and Double wrapper classes.
    • Note that here we did not have to import the NumberFormatException class because it is part of the java.lang package, which means that it comes built in.
    • We also did not have to clear the buffer using userInputScanner.nextLine(); in the catch part of the try-catch.

Tips

  • See the Scanner API for more information on using the Scanner class.

Related Articles