How to Fix Scanner.nextLine() getting skipped after nextInt() in Java
Problem
When I run a Java program that reads both integer and string input using Scanner.nextLine(), I got this error:
Enter your age: 25Enter your name:Age: 25Name:Notice how the name input is completely skipped! The program asks for the name but immediately proceeds without letting me type anything.
Environment
- Java 21
- Windows 11 / macOS / Linux (cross-platform issue)
- java.util.Scanner class
What happened?
I was writing a simple Java program that asks for user’s age and name. 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 integer from input
System.out.print("Enter your name: "); String name = scanner.nextLine(); // Gets skipped!
System.out.println("Age: " + age); System.out.println("Name: " + name); }}I can explain the key parts:
scanner.nextInt()reads the integer value from inputscanner.nextLine()should read the entire line including the name- Both seem straightforward and should work
But when I run this program and enter 25 for age, the name input gets completely skipped.
How to solve it?
I tried to add a simple scanner.nextLine() after nextInt():
import java.util.Scanner;
public class Solution1 { 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 newline character
System.out.print("Enter your name: "); String name = scanner.nextLine(); // Now works properly
System.out.println("Age: " + age); System.out.println("Name: " + name); }}What changed and why: I added an extra scanner.nextLine() call right after nextInt(). This consumes the newline character left in the buffer.
Now test again:
Enter your age: 25Enter your name: John DoeAge: 25Name: John DoeYou can see that I succeeded to get both inputs working properly.
The reason
I think the key reason for the error is:
nextInt()only reads the numeric token (25) from the input- It leaves the newline character (
\n) in the Scanner’s buffer - The next
nextLine()immediately reads this empty line and returns - The program never waits for actual user input for the name
This happens because Scanner’s token-based methods (nextInt(), next(), etc.) work differently from line-based methods (nextLine()). Token-based methods read until they find a delimiter (whitespace, newline, etc.) but don’t consume the delimiter itself.
Summary
In this post, I showed how to resolve Scanner.nextLine() getting skipped when using nextInt() in Java. The key point is understanding Scanner’s buffer behavior with token-based methods versus line-based methods. The simplest fix is to add an extra nextLine() call after nextInt() to consume the newline character, but you can also use Integer.parseInt() with nextLine() for a more consistent approach.
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