Anwar Haredy

Software Engieering and Computer Science

The Solution to the Scanner.nextLine Problem

The problem

After you use a Scanner.next() in a Java console program, the next Scanner.nextLine() method appears to be skipped entirely when running the program.

The cause

When pressing "Enter" to feed your program an input via Scanner.next() or any of its sister methods, you create a newline ("\n") character that is not consumed by your Scanner. Now, the next time Scanner.nextLine() is called, it consumes the "\n" character instead and appears to be entirely skipped.

The solution

One way to solve this problem is to simply use an additional Scanner.nextLine() after every Scanner.next() call.

Scanner input = new Scanner(System.in);

String phoneNumber = input.next();

input.nextLine() //Consumes the newline character
String fullName = input.nextLine()

This will force the first instance of nextLine to consume the additional "\n" character and free the other one to do its job. However, there are two problems with this solution.

  • It does not look pleasant or elegant.
  • It may create problems if you are using loops in your program.

The best solution in my opinion is to use Scanner.nextLine() to read every single input in your program. And then just parse as required.

int numberOfDays = Integer.parseInt(input.nextLine());
double cost = Double.parseDouble(input.nextLine());
String fullName = input.nextLine();

//...and so on

This neatly solves the problem!