HOWTO · Java
How to Fix Java Error: Scanner NextLine Skips
This article discusses why do the Java Scanner NextLine skips occur and how to fix it
On this page
When Scanner.nextLine() seems to skip a name after a number, a common cause is mixing a token read with a line read. Scanner.nextInt() reads the integer token but leaves the rest of that line for the next read. The following Scanner.nextLine() returns that remainder, often an empty string, instead of reaching the next line with the name. Read the number as a whole line with Integer.parseInt(), or explicitly consume the current line remainder before reading the name.
Why Scanner.nextLine() Appears to Skip Input
This runnable example uses two lines of sample input: 42 and Ada Lovelace. nextInt() obtains the number, and the immediately following nextLine() returns the remainder of the number’s line. The printed Name: [] shows the returned value; the name line is still unread.
Run this example:
import java.util.Scanner;
public class ScannerSkipExample {
public static void main(String[] args) {
Scanner scanner = new Scanner("42\nAda Lovelace\n");
int number = scanner.nextInt();
String name = scanner.nextLine();
System.out.println("Integer: " + number);
System.out.println("Name: [" + name + "]");
}
}
Integer: 42
Name: []
How Scanner Reads Tokens and Lines
Scanner offers both token-oriented and line-oriented methods. next() reads the next token, while nextInt() reads and converts the next token to an integer. Neither consumes the rest of the line after that token. nextLine() returns the remaining characters on the current line and advances past its line separator. When the token is followed immediately by a line separator, that remainder contains no visible characters, so nextLine() returns an empty string.
This is not a special rule that makes nextLine() skip input. It is the result of asking for the current line remainder when the program expected a later line. The same issue can occur after other token-reading methods, such as nextDouble(). The Java Scanner documentation describes these input methods.
Fix the Skipped Line by Reading the Whole Line
When input has a number on one line and a name on the next, read both as lines. Convert only the numeric line with Integer.parseInt(). This example uses the same sample values and preserves spaces in the name.
Run this example:
import java.util.Scanner;
public class ScannerWholeLineExample {
public static void main(String[] args) {
Scanner scanner = new Scanner("42\nAda Lovelace\n");
int number = Integer.parseInt(scanner.nextLine());
String name = scanner.nextLine();
System.out.println("Integer: " + number);
System.out.println("Name: " + name);
}
}
Integer: 42
Name: Ada Lovelace
With both reads consuming complete lines, the second read gets the full name, including spaces. Integer.parseInt() throws NumberFormatException if the first line is not a valid integer; add input validation and a retry loop if the program should recover instead of stopping. This approach is especially straightforward when the application treats each answer as one line.
Fix the Skipped Line by Consuming the Rest of the Current Line
If the program already uses nextInt(), call nextLine() once immediately afterward to consume the remainder of the number’s line. Then call it again to read the intended full-line value. The first call is deliberate; the second reads the name.
Run this example:
import java.util.Scanner;
public class ScannerConsumeLineExample {
public static void main(String[] args) {
Scanner scanner = new Scanner("42\nAda Lovelace\n");
int number = scanner.nextInt();
scanner.nextLine();
String name = scanner.nextLine();
System.out.println("Integer: " + number);
System.out.println("Name: " + name);
}
}
Integer: 42
Name: Ada Lovelace
Use this pattern when token-based parsing is useful for the number and the rest of that input line is disposable. If meaningful text can follow the number on the same line, the first nextLine() consumes that text too; read the whole line and parse its numeric part instead.
trim() Does Not Consume the Pending Line
trim() operates on a String that has already been read. It removes leading and trailing characters at or below the space character from that returned string; it does not read from System.in, consume a pending line remainder, or change which line the next Scanner call will read. Calling scanner.nextLine().trim() immediately after nextInt() therefore still trims the remainder of the number’s line, often producing an empty string. It does not retrieve the name from the next line.
Use one of the input-order fixes above first. Apply trim() afterward only when the application wants to remove surrounding whitespace from the line it actually read. Keep code and results as selectable text when diagnosing input behavior, so readers can compare the sequence of calls directly.