NumberFormatException
Java Programming Language
Severity: MinorWhat Does This Error Mean?
A NumberFormatException means Java tried to convert a String into a number but the string did not contain a valid number. For example, calling Integer.parseInt('hello') fails because 'hello' is not a number. This is a very common error when reading user input or data from files, because those values are not always what you expect.
Affected Models
- Java 8
- Java 11
- Java 17
- Java 21
- All Java versions
Common Causes
- Calling Integer.parseInt() or Double.parseDouble() on a string that contains letters or special characters
- The string has extra whitespace — a space before or after the number (like ' 42') can cause this
- The string contains a decimal point but you are trying to parse it as an integer (like Integer.parseInt('3.14'))
- The number in the string is too large to fit in the target type (like parsing a very large number into an int)
- The string is empty or null — calling parseInt on an empty string or null always throws this error
How to Fix It
-
Read the error message — it includes the actual string that caused the problem. For example: 'For input string: "hello"'. This tells you exactly what value was passed.
If the string came from user input, a file, or an API response, it may contain characters you did not expect.
-
Trim the string before parsing to remove leading and trailing whitespace: Integer.parseInt(input.trim()). A single hidden space is a very common cause of this error.
Also consider calling replaceAll('\s+', '') to remove all whitespace if the input might have spaces inside the number.
-
Wrap parsing in a try/catch block to handle invalid input gracefully instead of crashing. Show the user a friendly message or use a default value when parsing fails.
Example: try { int value = Integer.parseInt(input.trim()); } catch (NumberFormatException e) { System.out.println('Please enter a valid number.'); }
-
If you are parsing a decimal number, use Double.parseDouble() or Float.parseFloat() instead of Integer.parseInt(). Integer parsing rejects decimal points.
If you want an integer from '3.14', first parse as double then cast: (int) Double.parseDouble('3.14') — this gives you 3.
-
For safer parsing without try/catch, consider using a utility method or Apache Commons NumberUtils.toInt(str, defaultValue) which returns a default value instead of throwing when parsing fails.
Or write your own helper: private int parseOrDefault(String s, int def) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { return def; } }
When to Call a Professional
NumberFormatException is always a fixable code issue. The error message includes the string that failed to parse. The most important fix is to validate or sanitize input before trying to parse it.
Frequently Asked Questions
What is the difference between NumberFormatException and ArithmeticException?
NumberFormatException happens when converting a string to a number — the string content is invalid. ArithmeticException happens during a math operation — like dividing by zero. One is about parsing text, the other is about calculating numbers.
How do I check if a string is a valid number before parsing it?
You can check with a regular expression: str.matches('-?\d+') checks for a valid integer. Or use try/catch and handle the exception — this is the standard Java approach. There is no built-in 'isNumeric' method in standard Java, but Apache Commons Lang has StringUtils.isNumeric().
Why does Integer.parseInt('3.0') throw an exception when the value is technically a whole number?
Because '3.0' contains a decimal point, which is not valid syntax for an integer string. Integer.parseInt() expects only digits, an optional leading minus sign, and nothing else. If you have '3.0', first use Double.parseDouble('3.0') to get 3.0, then cast to int to get 3.