java
type_error
ai_generated
true
java.lang.NumberFormatException: For input string: "..."
ID: java/numberformatexception-for-input-string
90%Fix Rate
90%Confidence
1Evidence
2023-03-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
Root Cause
Attempting to parse a string that does not contain a parsable number, often due to whitespace, null, or unexpected characters.
generic中文
尝试解析一个不包含可解析数字的字符串,通常由于空白字符、null 或意外字符。
Official Documentation
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/NumberFormatException.htmlWorkarounds
-
90% success Use try-catch with NumberFormatException and provide a default value: 'int value; try { value = Integer.parseInt(input.trim()); } catch (NumberFormatException e) { value = 0; }'
Use try-catch with NumberFormatException and provide a default value: 'int value; try { value = Integer.parseInt(input.trim()); } catch (NumberFormatException e) { value = 0; }' -
85% success Validate input with a regex before parsing: 'if (input != null && input.matches("\\d+")) { int value = Integer.parseInt(input); } else { /* handle error */ }'
Validate input with a regex before parsing: 'if (input != null && input.matches("\\d+")) { int value = Integer.parseInt(input); } else { /* handle error */ }'
中文步骤
Use try-catch with NumberFormatException and provide a default value: 'int value; try { value = Integer.parseInt(input.trim()); } catch (NumberFormatException e) { value = 0; }'Validate input with a regex before parsing: 'if (input != null && input.matches("\\d+")) { int value = Integer.parseInt(input); } else { /* handle error */ }'
Dead Ends
Common approaches that don't work:
-
95% fail
Using Integer.valueOf() without try-catch on user input will crash the application if input is invalid.
-
80% fail
Assuming locale-specific number formats (e.g., commas in European locales) are supported by default can cause parsing failures.