# java.lang.NumberFormatException: 对于输入字符串: "..."

- **ID:** `java/numberformatexception-for-input-string`
- **领域:** java
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

尝试解析一个不包含可解析数字的字符串，通常由于空白字符、null 或意外字符。

## 版本兼容性

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

## 解决方案

1. ```
   Use try-catch with NumberFormatException and provide a default value: 'int value; try { value = Integer.parseInt(input.trim()); } catch (NumberFormatException e) { value = 0; }'
   ```
2. ```
   Validate input with a regex before parsing: 'if (input != null && input.matches("\\d+")) { int value = Integer.parseInt(input); } else { /* handle error */ }'
   ```

## 无效尝试

- **** — Using Integer.valueOf() without try-catch on user input will crash the application if input is invalid. (95% 失败率)
- **** — Assuming locale-specific number formats (e.g., commas in European locales) are supported by default can cause parsing failures. (80% 失败率)
