# java.util.InputMismatchException

- **ID:** `java/inputmismatchexception`
- **领域:** java
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

使用 Scanner 时输入令牌与期望类型不匹配，通常由于区域设置错误或数据格式意外。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   Use hasNextInt() before nextInt(): 'if (scanner.hasNextInt()) { int value = scanner.nextInt(); } else { scanner.next(); /* consume invalid token */ }'
   ```
2. ```
   Set the locale to match the input format: 'scanner.useLocale(Locale.US);' for decimal numbers with dot separator.
   ```

## 无效尝试

- **** — Setting the locale to US (Locale.US) may not fix the issue if the input contains non-numeric characters. (60% 失败率)
- **** — Using next() instead of nextInt() and then manually parsing can still throw NumberFormatException if the string is malformed. (70% 失败率)
