NumberFormatException in Java — What It Means and How to Fix It
What Java's NumberFormatException means and how to fix it.
Published November 2, 2026
NumberFormatException is thrown when you try to convert a string that is not a valid number, e.g. Integer.parseInt("abc").
What it means and why it happens
Typical causes: empty strings, spaces, non-numeric characters, wrong decimal separators or values out of range.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
java.lang.NumberFormatException: For input string: "abc"
Practical examples
Code that triggers it
int n = Integer.parseInt("12a");The fix
try {
int n = Integer.parseInt(input.trim());
} catch (NumberFormatException e) {
n = 0;
}Validate with regex
if (input.matches("-?\\d+")) { n = Integer.parseInt(input); }Common mistakes
- Not trimming input.
- Parsing null (also throws).
- Using parseInt for values beyond int range.
Key takeaways
- NumberFormatException: NumberFormatException is thrown when you try to convert a string that is not a valid number, e.g.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
parseInt vs valueOf?
parseInt returns a primitive; valueOf returns an Integer object.
How do I find which line caused NumberFormatException?
The error message and stack trace show the file and line; start there and work backwards.