NumberFormatException in Java — How to Fix (2026) | Tutorials Logic
What is This Error?
The NumberFormatException is thrown when you try to convert a String to a numeric type (int, long, double, etc.) but the string doesn't contain a valid number. It's a subclass of IllegalArgumentException.
Error Message:
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"java.lang.NumberFormatException: For input string: "12.5" (when parsing int)
Common Causes
Quick Fix (TL;DR)
// ❌ Problem
int num = Integer.parseInt("abc"); // NumberFormatException!
// ✅ Solution 1: try-catch
try {
int num = Integer.parseInt(input.trim());
} catch (NumberFormatException e) {
System.out.println("Invalid number: " + input);
}
// ✅ Solution 2: Validate first
if (input.matches("-?\\d+")) {
int num = Integer.parseInt(input);
}
Common Scenarios & Solutions
Scenario 1: User Input Validation
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
String input = scanner.nextLine();
int age = Integer.parseInt(input); // Crashes if user types "twenty"!
Scanner scanner = new Scanner(System.in);
int age = -1;
while (age < 0) {
System.out.print("Enter age: ");
String input = scanner.nextLine().trim();
try {
age = Integer.parseInt(input);
if (age < 0 || age > 150) {
System.out.println("Please enter a valid age (0-150)");
age = -1;
}
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
}
}
System.out.println("Age: " + age);
Scenario 2: Parsing Decimal as Integer
String price = "19.99";
int p = Integer.parseInt(price); // NumberFormatException! "19.99" is not an int
String price = "19.99";
// ✅ Parse as double
double p = Double.parseDouble(price); // 19.99
// ✅ Or parse as int after truncating
int pInt = (int) Double.parseDouble(price); // 19
// ✅ Use BigDecimal for money
BigDecimal bd = new BigDecimal(price); // Exact representation
Scenario 3: Whitespace in String
String input = " 42 "; // Has spaces!
int num = Integer.parseInt(input); // NumberFormatException!
String input = " 42 ";
int num = Integer.parseInt(input.trim()); // ✅ Always trim first!
Scenario 4: Safe Parsing Helper Method
// ✅ Reusable safe parse method
public static Optional safeParseInt(String s) {
if (s == null || s.trim().isEmpty()) return Optional.empty();
try {
return Optional.of(Integer.parseInt(s.trim()));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
// Usage
safeParseInt("42").ifPresent(n -> System.out.println("Parsed: " + n));
safeParseInt("abc").ifPresentOrElse(
n -> System.out.println(n),
() -> System.out.println("Invalid number")
);
Best Practices to Avoid This Error
Related Errors
Key Takeaways
- NumberFormatException is thrown when a String cannot be converted to a number
- Always call trim() before parsing to remove whitespace
- Use parseInt for integers, parseDouble for decimals — don't mix them
- Wrap parsing in try-catch when handling user input
- Use regex validation before parsing for cleaner code
- Use BigDecimal for monetary values to avoid floating-point precision issues
Frequently Asked Questions
Level Up Your Core java Skills
Master Core java with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related Java Topics