Keyboard Input With The Scanner Class

The Scanner class was introduced in version 5.0 of the JDK. The Scanner class provides methods for reading byte, short, int, long, float, double, and String data types from the keyboard.

In order to use the Scanner class you must import the java.util package into your program using the following statement:

   import java.util.Scanner;

In order to use the Scanner class, you must first instantiate (declare) a Scanner object and associate it with a data source. The data source specifies the input device that will be used for this Scanner object. The data source we will use will be the keyboard, which is called The following statement will instantiate a Scanner object named keyboard (we chose the identifier keyboard) and associate as the data source.

   Scanner keyboard = new Scanner(;

Once the Scanner object has been instantiated, you can use it to call any of the next... methods to input data from the keyboard. There is a next... method for each of the basic data types. The specific next... method you call depends upon the type of input you want from the user. Each of the next... methods returns a value from the input stream. You will need to assign the return value from the next... method to a variable to complete the data input.

   int age;
   // prompt the user for input
   System.out.println("Please enter your age: ");
   // read the user input
   age = keyboard.nextInt();

A Scanner object divides its input into sequences of characters called tokens, using delimiters. A delimiter is a character that is used to separate other characters. The default delimiters are the standard whitespace characters: space, tab, and newline.

By default, when a Scanner object tokenizes the input, it skips leading whitespace, then builds a token composed of all subsequent characters that do not match its delimiters until it encounters another delimiter. In other words, it separates the input into "words", although the words may contain characters other than letters.

An input line can contain more than one token. For example, if we prompt the user for her name and age, and the user enters the following line, and then presses Enter:

then the leading whitespace is skipped and the Scanner object creates three tokens:


If the user enters the input on multiple lines, the Scanner methods will still find the tokens, because newline is a whitespace character. For example, if the user enters:


the same three tokens will be created by the Scanner object.

Note that the comma becomes part of the second token. To input these three tokens, your program would use two calls to the next method to retrieve the two String tokens and a call to nextInt to retrieve age.

   int age;
   String firstName;
   String lastName;
   Scanner keyboard = new Scanner(;

   // prompt the user for input
   System.out.println("Please enter your name and age: ");

   // read the user input
   firstName =;
   lastName =;
   age = keyboard.nextInt();

If the userís input (that is, the next token) does not match the data type of the next... method call, then an InputMismatchException is generated. This is a runtime error which terminates execution of the program.

If the user doesnít type anything when prompted, or if the user types some characters but doesnít press Enter, the program will simply wait until the user does press Enter. If you don't use a print or println statement to prompt the user before reading, he/she might not realize the computer is waiting for input. Every time you read using Scanner you need to use a print statement before the read, to tell the user what to enter.


Scanner Methods

The following table shows the Scanner method used to read in data of each of the basic types.

Type Method
byte nextByte
double nextDouble
float nextFloat
int nextInt
long nextLong
short nextShort
String next
String nextLine

The nextLine method reads all data on the line into one String.


   import java.util.Scanner;

   public class DataInput {
      public static void main(String [] args) {
         Scanner scan = new Scanner(;
         String firstName;
         int age;
         float gpa;

         System.out.print("Please enter your first name: ");
         firstName =;

         System.out.print("Please enter your age as an integer: ");
         age = scan.nextInt();

         System.out.print("Please enter your GPA: ");

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013