next() vs nextLine() and the Enter key
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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
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(); }}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, andDelhistays 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.
Trace the code
The bug: nextInt() then nextLine()
Variables
None yet.
Output
Enter your age:
Typed input: 25
Line 6The first prompt is printed.
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
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(); }}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 nonextLine()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()
Variables
None yet.
Output
Enter your age:
Typed input: 25 ⏎ Ravi Kumar
Line 6The first prompt is printed.
Next step
Check your understanding
9 quick questions