InputMismatchException ☕ Java

java.util.InputMismatchException (Scanner)

Scanner.nextInt()/nextDouble() read a token that isn’t that type.

Seen on: Java

Meaning

Typing text where a number is expected, locale decimal separators (3,5 vs 3.5) or leftover newline handling cause it.

Common causes

  • Non-numeric input
  • Locale using comma decimals
  • Mixing nextInt() and nextLine()

⚡ Quick fix

  1. Check hasNextInt() before nextInt()
  2. Set scanner.useLocale(Locale.US)
  3. Read lines and parse with validation

Detailed fix by platform

Java

  1. while (!sc.hasNextInt()) { sc.next(); System.out.print("Enter a number: "); }

How to diagnose

  1. Input — What token was read?
  2. Locale — Decimal separator?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.