Why Scanner.nextInt() Skips nextLine() Input in Java: The Complete Guide
Problem
When I write Java console applications that mix number and text input, I get this frustrating behavior:
// Problem: nextLine() is skippedimport java.util.Scanner;
public class ScannerProblem { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: "); int age = scanner.nextInt(); // Reads only the number, leaves \n
System.out.print("Enter your name: "); String name = scanner.nextLine(); // Immediately reads the leftover \n!
System.out.println("Age: " + age + ", Name: '" + name + "'"); scanner.close(); }}When I run this, I get:
Enter your age: 25Enter your name: Age: 25, Name: ''The name input is completely skipped! The program doesn’t wait for user input after nextInt().
Environment
- Java 21
- macOS Sonnet
- Standard console input
What happened?
I was building a simple console application that asks for user age and name. The code seems logical - read an integer, then read a string. But when I run it, the nextLine() method doesn’t wait for input.
Here’s my setup:
import java.util.Scanner;
public class ScannerProblem { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: "); int age = scanner.nextInt(); // Reads only the number, leaves \n
System.out.print("Enter your name: "); String name = scanner.nextLine(); // Immediately reads the leftover \n!
System.out.println("Age: " + age + ", Name: '" + name + "'"); scanner.close(); }}I can explain the key parts:
scanner.nextInt()reads the integer value from inputscanner.nextLine()should read the entire line including newline character
But when I run the program, the nextLine() doesn’t wait for input. It immediately reads the leftover newline character that nextInt() left behind.
How to solve it?
I tried adding a dummy nextLine() call to clear the buffer:
import java.util.Scanner;
public class ScannerSolution1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.print("Enter your age: "); int age = scanner.nextInt(); scanner.nextLine(); // Consume the leftover newline
System.out.print("Enter your name: "); String name = scanner.nextLine();
System.out.println("Age: " + age + ", Name: '" + name + "'"); scanner.close(); }}[Explain why you tried this - brief] I read somewhere that Scanner methods can leave newline characters in the buffer, so I tried adding an extra nextLine() call to consume it.
Now test again:
Enter your age: 25Enter your name: AliceAge: 25, Name: 'Alice'You can see that I succeeded to get proper input for both fields.
The reason
I think the key reason for this error is:
nextInt()only reads the integer value but leaves the newline character (\n) in the input buffer- When
nextLine()is called, it immediately reads everything up to the next newline character, which is the leftover from the previous input - This makes it appear that nextLine() was skipped when it actually consumed the leftover newline
- Different Scanner methods have different delimiters - nextInt() stops at whitespace while nextLine() reads until newline
Summary
In this post, I demonstrated why Scanner.nextInt() followed by nextLine() skips the nextLine() input and how to fix this common Java input handling issue. The key point is understanding that Scanner’s input buffer behavior causes this issue, and you need to either consume the leftover newline or use nextLine() exclusively for all input.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments