Skip to lesson

Common input errors

An input error happens when the user types something the program does not expect, or when the reading code has a small slip. Each common one has a clear message, a simple cause and a one-line fix.

Text where a number is expected

You ask Enter your age: and the user types twenty. The program stops with a message that starts with InputMismatchException.

An exception is an error while the program runs. Mismatch means the typed value does not fit the type: nextInt() reads only whole numbers.

Example 1 · Typing a word for nextInt()

AgeCheck.javaJava
import java.util.Scanner;
public class AgeCheck {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Enter your age: ");        int age = sc.nextInt();        System.out.println("Age: " + age);        sc.close();    }}
Sample output
Enter your age: twenty
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:943)
	at java.base/java.util.Scanner.next(Scanner.java:1598)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2263)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2217)
	at AgeCheck.main(AgeCheck.java:7)

What this code does

  • The user typed twenty, but nextInt() needs digits, so the program stops at line 7, the last at line.
  • Age: is never printed. The numbers in the java.base lines depend on your Java version.

Prevent it with a prompt that says what to type, and read with nextDouble() when decimals are possible. What are exceptions? (opens in a new tab) shows how a program can catch the error and ask again.

Review: Reading numbers, words and characters (opens in a new tab)

Forgot the import

You copy a Scanner program into a new file, and javac refuses it with cannot find symbol and symbol: class Scanner.

Without import java.util.Scanner; at the top of the file, Java does not know what Scanner means.

Example 2 · Missing import

Greet.javaJava
public class Greet {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Enter your name: ");        String name = sc.nextLine();        System.out.println("Hi, " + name);        sc.close();    }}
Sample compiler output
Greet.java:3: error: cannot find symbol
        Scanner sc = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Greet
Greet.java:3: error: cannot find symbol
        Scanner sc = new Scanner(System.in);
                         ^
  symbol:   class Scanner
  location: class Greet
2 errors

What this code does

  • javac stops at line 3 because it does not know the class Scanner.
  • Both errors point at the same line, one per Scanner word; one import line fixes both.

Add the import line above the class and both errors go away.

Review: Reading input with Scanner (opens in a new tab)

The program seems stuck

You run the program and the screen stays empty; it neither prints nor finishes. It is almost always waiting for input at a read like nextInt(), and nothing told you so.

Print a prompt before every read. In an IDE, click inside the console or terminal panel before you type, so your keys go to the program and not to the code editor.

Review: Reading input with Scanner (opens in a new tab)

Reading after close()

A program reads the age, closes the Scanner, and then tries to read the name. It stops with IllegalStateException: Scanner closed.

Java
Scanner sc = new Scanner(System.in);int age = sc.nextInt();sc.close();String name = sc.next();   // IllegalStateException: Scanner closed

After sc.close() the Scanner cannot read any more. Keep one Scanner for the whole program and call sc.close() once, after the last read.

Review: Reading input with Scanner (opens in a new tab)

Spotting Scanner mistakes before you run

Most input bugs come from the same few slips. Each round below has one wrong line: find it before you read the answer. The skipped name after a number is explained in next() vs nextLine() (opens in a new tab).

Find the bug

Find the wrong line

Select the line that is wrong.

This file does not compile: cannot find symbol, class Scanner.

Round 1 of 6

Next step

Check your understanding

8 quick questions