Skip to main content

next() vs nextLine() in Java Scanner

5 min readUpdated

Java's Scanner reads input in two ways. next() reads one word. nextLine() reads the rest of the current line. Mixing nextInt() with nextLine() causes the most common Scanner bug, and this guide shows why and how to fix it.

The difference in one table

MethodReadsStops atExample inputReturns
next()One word (token)The next space, tab or newlineAda Lovelace"Ada"
nextLine()The rest of the lineThe end of the line (newline is used up, not returned)Ada Lovelace"Ada Lovelace"

See both in a program

This program reads from a string instead of the keyboard so you can see the exact result. next() stops at the first space; nextLine() then takes everything left on that line, including the leading space.

NextVsNextLine.java
import java.util.Scanner;

public class NextVsNextLine {
    public static void main(String[] args) {
        Scanner in = new Scanner("Ada Lovelace\nAlan Turing\n");
        String word = in.next();
        String rest = in.nextLine();
        String line = in.nextLine();
        System.out.println("next(): [" + word + "]");
        System.out.println("nextLine(): [" + rest + "]");
        System.out.println("nextLine(): [" + line + "]");
    }
}

Output

next(): [Ada]
nextLine(): [ Lovelace]
nextLine(): [Alan Turing]

Why nextLine() returns an empty string after nextInt()

nextInt() reads the number but leaves the newline after it in the input. The next nextLine() call finds that newline straight away and returns an empty string, so it looks as if your program skipped the question.

SkippedLine.java
import java.util.Scanner;

public class SkippedLine {
    public static void main(String[] args) {
        Scanner in = new Scanner("17\nGrace Hopper\n");
        int age = in.nextInt();
        String name = in.nextLine();
        System.out.println("age = " + age);
        System.out.println("name = [" + name + "]");
    }
}

Output

age = 17
name = []

Two ways to fix it

  1. Call nextLine() once after nextInt() to use up the leftover newline, then call it again for the real line.
  2. Read every line with nextLine() and convert numbers yourself with Integer.parseInt(...). This avoids the problem completely and is the habit most teachers recommend.
FixedInput.java
import java.util.Scanner;

public class FixedInput {
    public static void main(String[] args) {
        Scanner in = new Scanner("17\nGrace Hopper\n");
        int age = Integer.parseInt(in.nextLine().trim());
        String name = in.nextLine();
        System.out.println("age = " + age);
        System.out.println("name = [" + name + "]");
    }
}

Output

age = 17
name = [Grace Hopper]

In AP Computer Science A you will not be asked to read keyboard input, but the same idea (what a method consumes and what it leaves behind) appears in String and loop questions.

Frequently asked questions

Does nextLine() return the newline character?

No. It reads up to the end of the line, uses up the newline and returns the text without it.

Why does my program skip the name question after asking for a number?

nextInt() leaves the newline behind, so the next nextLine() returns an empty string at once. Add an extra nextLine() after nextInt(), or read every line with nextLine() and parse numbers yourself.

Should I use next() or nextLine() for a full name?

Use nextLine(). next() stops at the first space, so it would only return the first name.

Practise this, step by step

Short lessons with quizzes, practice and interview questions, from your first program to job-ready.

Java Programming course: syllabus and 1:1 mentoringCore Java from zero to OOP, collections, streams, files and threads.