Skip to lesson

next() vs nextLine() and the Enter key

next() reads one word and stops at a space. nextLine() reads everything up to the end of the line, spaces included, and that difference causes the most famous Scanner bug.

next() or nextLine()?

A form asks for a full name. The user types Ravi Kumar, but the program keeps only Ravi.

next() reads one word and stops at a space; nextLine() reads the whole line, spaces included.

Example 1 · next() stops at a space

NameAndCity.javaJava
import java.util.Scanner;
public class NameAndCity {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Full name: ");        String fullName = sc.nextLine();        System.out.print("City: ");        String city = sc.next();        System.out.println("Name: " + fullName);        System.out.println("City: " + city);        sc.close();    }}
Sample output
Full name: Ravi Kumar
City: New Delhi
Name: Ravi Kumar
City: New

What this code does

  • nextLine() reads the whole name, Ravi Kumar, space included.
  • next() reads only the first word, New, and Delhi stays unread.

Use nextLine() for anything that can have spaces: a full name, a city like New Delhi, an address or a sentence. Use next() only when you want one word.

The leftover Enter

Now the form asks for the age first and the name second. The program prints the name question and finishes at once: the user never types a name.

Pressing Enter adds an invisible line end (⏎) after 25. nextInt() stops in front of it, so the next nextLine() finds it at once and returns an empty String "". Watch it in the picture, then in the real run.

Picture 1 / 4
The input holds three characters: 2, 5 and the line end added by the Enter key. The Scanner starts before the 2.
Picture 1 / 4

Trace the code

The bug: nextInt() then nextLine()

import java.util.Scanner;
public class SkipProblem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Name: [" + name + "], age: " + age);
sc.close();
}
}

Variables

None yet.

Output

Enter your age: 

Typed input: 25

Line 6The first prompt is printed.

Step 1 of 6
The same happens after nextDouble(), nextLong(), nextBoolean() and next(): they all leave the line end. Only a nextLine() that comes next is hurt; next() and nextInt() skip line ends by themselves.

Two simple fixes

Fix 1: right after reading the number, call sc.nextLine(); once and ignore the result; it throws away the leftover line end.

Fix 2: when the order does not matter, read the text first. The program below uses Fix 2.

Example 2 · Fix 2: read the text first

TextFirst.javaJava
import java.util.Scanner;
public class TextFirst {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Enter your name: ");        String name = sc.nextLine();        System.out.print("Enter your age: ");        int age = sc.nextInt();        System.out.println("Name: [" + name + "], age: " + age);        sc.close();    }}
Sample output
Enter your name: Ravi Kumar
Enter your age: 25
Name: [Ravi Kumar], age: 25

What this code does

  • nextLine() reads the name and its line end together, so nothing is left behind.
  • nextInt() comes last; the line end it leaves does not matter because no nextLine() follows.

Now step through Fix 1: the extra sc.nextLine() reads the line end (the last frame of the picture above), so the next nextLine() waits for the user.

Trace the code

The fix: one extra nextLine()

import java.util.Scanner;
public class SkipFixed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
sc.nextLine(); // throw away the leftover line end
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Name: [" + name + "], age: " + age);
sc.close();
}
}

Variables

None yet.

Output

Enter your age: 

Typed input: 25 ⏎ Ravi Kumar

Line 6The first prompt is printed.

Step 1 of 7

Next step

Check your understanding

9 quick questions