NumberFormatException in Java
Fast recall
Quick revision
Core answer
Practical awareness of input validation and exception handling when processing user input or external data
Not wrapping parse calls in try-catch when processing user input, causing the application to crash on invalid data
Show the correct pattern with try-catch, provide a default value, and mention input validation before parsing
Interview-ready guide
Interview answer
parseDouble, but the string doesn't represent a valid number.
For example, if you try to parse the string abc as an integer, it throws NumberFormatException because abc isn't a number. parseInt, because parseInt expects whole numbers without decimal points. This exception is super common in real applications because you're always receiving data as strings — user input from web forms, parameters from API requests, values from configuration files.
You can't trust that data to be valid. If you just call parseInt on user input without handling the exception, the first time someone types something invalid your app crashes. The correct pattern is wrapping the parse call in a try-catch block.
empty to let the caller decide what to do. For REST APIs, you'd catch it and throw a domain exception that your handler converts to a four hundred bad request. One mistake freshers make is not trimming whitespace before parsing.
If the input has spaces like space one two three space, parseInt throws NumberFormatException even though one two three is valid. Always call trim first. Another tip is validating the format before parsing if you have strict requirements, like checking with regex that a price has exactly two decimal places.
NumberFormatException in Java
Direct Answer
NumberFormatException is thrown when you try to parse a string into a numeric type using methods like Integer.parseInt or Double.parseDouble, but the string doesn't represent a valid number. For example, parsing 'abc' as an integer throws this exception because 'abc' isn't a valid integer.
Code Example
public class InputValidator {
public int parseQuantity(String input) {
// Pattern 1: try-catch with default value
try {
return Integer.parseInt(input);
} catch (NumberFormatException e) {
System.err.println("Invalid quantity: " + input);
return 0; // sensible default
}
}
public Optional<Integer> parseUserId(String input) {
// Pattern 2: return Optional for caller to handle
try {
return Optional.of(Integer.parseInt(input));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
public double parsePrice(String input) {
// Pattern 3: validate before parsing
if (input == null || input.isBlank()) {
throw new IllegalArgumentException("Price cannot be empty");
}
try {
var price = Double.parseDouble(input.trim());
if (price < 0) { // business validation
throw new IllegalArgumentException("Price cannot be negative");
}
return price;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid price format: " + input);
}
}
}
Common Mistakes & Pitfalls
- Not catching NumberFormatException: Freshers call Integer.parseInt on user input without try-catch, assuming the input will always be valid. The first time a user types a letter or leaves the field empty, the application crashes. Always wrap parsing of external data in try-catch or validate format first.
- Parsing without trimming whitespace: Calling parseInt on " 123 " with leading or trailing spaces throws NumberFormatException even though 123 is a valid number. Always call trim before parsing.
- Using parseInt for decimal numbers: Trying to parse "12.5" with Integer.parseInt throws NumberFormatException because integers don't have decimal points. Use Double.parseDouble or Float.parseFloat for decimal numbers, or round the input first if you need an integer.
Check your understanding
Answer these without looking back at the article.
- What exception does Integer.parseInt throw for null input?
- How does Spring MVC handle invalid type conversion in @RequestParam?
- What is the parent class of NumberFormatException?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.