Ad Space — Top Banner

InputMismatchException

Java Programming Language

Severity: Moderate

What Does This Error Mean?

InputMismatchException is thrown by the Scanner class when the input it reads does not match the type you asked for. For example, calling scanner.nextInt() when the user types 'hello' instead of a number. The text 'hello' cannot be converted to an integer, so Scanner throws InputMismatchException. The fix is to read input as a String first, then validate and convert it yourself — or catch the exception and prompt the user again.

Affected Models

  • Java 5 and later
  • Java SE
  • Java EE
  • Android

Common Causes

  • Calling scanner.nextInt() when the user types a word or a decimal number
  • Calling scanner.nextDouble() when the input contains a comma instead of a period as the decimal separator
  • Reading a file with Scanner and expecting numbers but encountering text or empty lines
  • The locale affects how Scanner reads numbers — in some locales, 1.5 must be written as 1,5
  • Extra whitespace, newlines, or unexpected characters in the input stream

How to Fix It

  1. Wrap the scanner read in a try-catch block and prompt the user to try again when the input is wrong.

    Example: try { int n = scanner.nextInt(); } catch (InputMismatchException e) { scanner.next(); System.out.println('Please enter a whole number.'); }

  2. IMPORTANT: After catching InputMismatchException, call scanner.next() to consume the invalid token — if you do not, the scanner will keep trying to read the same bad input and loop forever.

    The scanner does not automatically move past the bad input when it throws. You must consume it manually with scanner.next() before the next read attempt.

  3. For more robust code, read input as a String with scanner.nextLine() and then convert it manually: String input = scanner.nextLine(); int value = Integer.parseInt(input.trim());

    Integer.parseInt() throws NumberFormatException if the string is not a number — which you can also catch and handle with a clear error message.

  4. Use scanner.hasNextInt() to check before calling scanner.nextInt(): if (scanner.hasNextInt()) { int n = scanner.nextInt(); } else { scanner.next(); System.out.println('Not a number'); }

    hasNextInt() returns true only if the next token can be read as an integer — no exception thrown.

  5. If you are reading from a file and the data format is inconsistent, consider reading all lines as Strings first and then parsing them, rather than using Scanner's type-specific methods.

    File data is often messier than expected — blank lines, extra spaces, or different formats. Reading as strings first gives you more control.

When to Call a Professional

InputMismatchException is always fixable yourself. The most robust approach is to always read input as a String and then convert and validate it manually. This gives you full control over error messages and retry logic.

Frequently Asked Questions

What is the Scanner class in Java?

Scanner is a built-in Java class that reads input from the console, a file, or a string. You create it with new Scanner(System.in) for console input. It has methods like nextInt(), nextDouble(), next(), and nextLine() that read different types of data. It is the standard way to get user input in beginner Java programs.

Why do I get InputMismatchException with decimal numbers in some countries?

Because Scanner uses your system's locale by default. In many European countries, decimal numbers use a comma: 1,5 instead of 1.5. If you type 1.5 and the scanner expects European format, it reads '1' as the number and '.' as an unexpected token. Fix: create the scanner with a specific locale: new Scanner(System.in).useLocale(Locale.US)

What is the difference between scanner.next() and scanner.nextLine()?

scanner.next() reads the next 'token' — one word, stopping at whitespace. scanner.nextLine() reads the entire line including spaces, up to the newline character. For reading a full sentence, use nextLine(). For reading one word or number, use next() or the specific type method. Be careful mixing nextInt() with nextLine() — nextInt() leaves the newline in the buffer, which nextLine() then immediately reads as an empty line.