Combining calls to nextInt and nextDouble with calls to nextLine requires special care. nextInt and nextDouble each read a numeric value. nextLine reads until a line separator (\n or \r\n).

Example with error

Scanner fromKeyboard = new Scanner(System.in);

System.out.print("What is your name?: ");
String name = fromKeyboard.nextLine();

System.out.print("What is your favorite color?: ");
String color = fromKeyboard.nextLine();

System.out.print("What is the airspeed velocity of an unladen swallow?: ");
double airspeedVelocity = fromKeyboard.nextDouble();

System.out.print("What is your quest?: ");
String quest = fromKeyboard.nextLine();

fromKeyboard.close();

System.out.println("Name: " + name);
System.out.println("Color: " + color);
System.out.println("Velocity: " + airspeedVelocity);
System.out.println("Quest: " + quest);

The user attempts to answer as shown below.

Brandon Horn
blue
20.1
to seek the Holy Grail

When the incorrect program is run, the behavior is as shown below.

What is your name?: Brandon Horn
What is your favorite color?: blue
What is the airspeed velocity of an unladen swallow?: 20.1
What is your quest?: Name: Brandon Horn
Color: blue
Velocity: 20.1
Quest: 

The user is able to answer the first 3 questions. The program does not allow the user to answer the last question.

Each of the first 2 questions calls nextLine to accept a response. Each call to nextLine reads from the input stream until a line separator is encountered. The line separator is processed (removed from the input stream). The line separator is not included in the value returned by nextLine.

The 3rd question calls nextDouble to accept a response. The user enters 4 visible characters (2, 0, ., and 1) and a line separator (by pressing Enter/Return) in response to the 3rd question. The 4 visible characters are processed by the call to nextDouble (and removed from the input stream). Although the line separator is not visible, it is part of the input stream. The call to nextDouble does not remove the line separator from the input stream.

The 4th question calls nextLine to accept a response. The call reads from the input stream until it encounters a line separator. The line separator from the previous response is next in the input stream. The call to nextLine removes the line separator from the stream and returns everything before the line separator (the empty String).

Corrected example

Scanner fromKeyboard = new Scanner(System.in);

System.out.print("What is your name?: ");
String name = fromKeyboard.nextLine();

System.out.print("What is your favorite color?: ");
String color = fromKeyboard.nextLine();

System.out.print("What is the airspeed velocity of an unladen swallow?: ");
double airspeedVelocity = fromKeyboard.nextDouble();
fromKeyboard.nextLine();  // removes line separator from input stream

System.out.print("What is your quest?: ");
String quest = fromKeyboard.nextLine();

fromKeyboard.close();

System.out.println("Name: " + name);
System.out.println("Color: " + color);
System.out.println("Velocity: " + airspeedVelocity);
System.out.println("Quest: " + quest);

The corrected program includes a call to nextLine after the call to nextDouble (for the 3rd question). The call to nextLine removes the line separator from the input stream.

The user can now answer the 4th question. When the corrected program is run, the behavior is as shown below.

What is your name?: Brandon Horn
What is your favorite color?: blue
What is the airspeed velocity of an unladen swallow?: 20.1
What is your quest?: to seek the Holy Grail
Name: Brandon Horn
Color: blue
Velocity: 20.1
Quest: to seek the Holy Grail

Comments

Comment on Keyboard input with mixed types