Stop 02 · Variables, Data Types & OperatorsTopics · 7/7

Reading Input with Scanner

Part of the Java Roadmap

In short

Programs get interesting the moment they respond to a person. Scanner is the simplest way to read what someone types, and it comes with one famous trap that every Java student falls into once.

The Scanner class reads input from the keyboard or files with methods like nextInt(), nextLine() and nextDouble(). Mixing nextInt() and nextLine() can skip input, a classic beginner bug.

The jaadu sits behind a bank counter window taking paper slips from a small chute, while one blank slip is left lying in the tray.

Think of it like this

The bank token counter

Everything a customer says goes into one queue at the counter. nextInt() takes just the number and leaves the "Enter" behind in the queue. The next person to call nextLine() picks up that leftover "Enter" and walks away with nothing. Scanner reads from one shared queue of typed characters.

Watch it happen

What happens when you type 21, Enter, Aarav, Enter

enqueue(21)
21

Front of the queue is coloured

1/5You type 21. It waits in the input buffer.

The big ideas

What you need to know

Create once

Scanner sc = new Scanner(System.in); Make one and reuse it for all input in the program.

Typed readers

nextInt(), nextDouble(), nextBoolean() read one token. next() reads one word, nextLine() reads the rest of the line.

The leftover newline

After nextInt(), call sc.nextLine() once to throw away the Enter before reading a full line.

Check before reading

hasNextInt() tells you whether the next token is a number, so bad input does not crash the program.

See it in code

Walk through the code

java
1import java.util.Scanner;2 3public class Signup {4    public static void main(String[] args) {5        Scanner sc = new Scanner(System.in);6 7        System.out.print("Age: ");8        while (!sc.hasNextInt()) {9            System.out.print("Please enter a number: ");10            sc.next(); // discard the bad token11        }12        int age = sc.nextInt();13        sc.nextLine(); // consume the leftover newline14 15        System.out.print("Full name: ");16        String name = sc.nextLine();17 18        System.out.print("Monthly budget (₹): ");19        double budget = sc.nextDouble();20 21        System.out.printf("%s, %d, budget ₹%.2f%n", name, age, budget);22    }23}

1/5Import Scanner and wrap System.in, the keyboard.

Validated number input, the newline fix and formatted output with printf.

Save yourself the pain

Common mistakes

Oops

nextInt() followed directly by nextLine() for a name.

Do this instead

The name comes back empty. Add an extra sc.nextLine() right after nextInt().

Oops

Creating a new Scanner(System.in) inside every method and closing it.

Do this instead

Closing it closes System.in for the whole program. Create one Scanner and pass it around.

Your mission

Ask and answer

~15 min

Quick check

After int n = sc.nextInt(); what does an immediate sc.nextLine() usually return?

Go deeper

  1. 1Build a program that reads two numbers and prints their sum.
  2. 2Reproduce the nextInt() then nextLine() skip bug and fix it.
  3. 3Close the Scanner when finished, or use try-with-resources later.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge