Common input errors
16 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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()
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(); }}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, butnextInt()needs digits, so the program stops at line 7, the lastatline. Age:is never printed. The numbers in thejava.baselines 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
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(); }}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 errorsWhat this code does
javacstops at line 3 because it does not know the classScanner.- Both errors point at the same line, one per
Scannerword; one import line fixes both.
Add the import line above the class and both errors go away.
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.
Reading after close()
A program reads the age, closes the Scanner, and then tries to read the name. It stops with IllegalStateException: Scanner closed.
Scanner sc = new Scanner(System.in);int age = sc.nextInt();sc.close();String name = sc.next(); // IllegalStateException: Scanner closedAfter sc.close() the Scanner cannot read any more. Keep one Scanner for the whole program and call sc.close() once, after the last read.
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 6Next step
Check your understanding
8 quick questions