Blog |

How to Handle the NumberFormatException in Java

How to Handle the NumberFormat<wbr>Exception in Java
Table of Contents

The NumberFormatException is one of the most common runtime exceptions you'll encounter in Java. It's an unchecked exception that occurs when you try to convert a string to a numeric value, but the string format isn't compatible with the target number type.

Simply put, if you attempt to parse "hello" as an integer or "12.5" as an integer, Java throws a NumberFormatException because these strings can't be converted to the expected numeric format.

Since it's an unchecked exception, you don't need to declare it in your method's throws clause, but you should handle it properly using try-catch blocks to prevent your program from crashing.

What Causes NumberFormatException?

Understanding the root causes helps you write more robust code. Here are the most common scenarios that trigger this exception:

1. Null Input String

Integer.parseInt(null); // Throws NumberFormatException

2. Empty Input String

Integer.parseInt(""); // Throws NumberFormatException

3. Whitespace-Only or Leading/Trailing Whitespaces

Integer.parseInt("   "); // Whitespace only
Integer.parseInt(" 123 "); // Leading/trailing spaces

Note: Integer.parseInt() doesn't automatically trim whitespace, but you can use String.trim() first.

4. Inappropriate Symbols or Formatting

Float.parseFloat("1,234"); // Comma separator not allowed
Integer.parseInt("$100"); // Currency symbols not allowed
Integer.parseInt("+123"); // Some parsers don't accept explicit + sign

5. Non-Numeric Characters

Integer.parseInt("Twenty Two"); // Text instead of numbers
Integer.parseInt("12abc"); // Mixed alphanumeric
Integer.parseInt("12.0"); // Decimal point in integer parsing

6. Values Outside the Data Type Range

Integer.parseInt("12345678901"); // Exceeds Integer.MAX_VALUE
Byte.parseByte("200"); // Exceeds Byte range (-128 to 127)

7. Data Type Mismatches

Integer.parseInt("12.34"); // Trying to parse decimal as integer
Integer.parseInt("1.5e10"); // Scientific notation as integer

 

Real-World Example and Stack Trace

Here's what happens when you encounter a NumberFormatException:

public class NumberFormatExceptionExample {
    public static void main(String[] args) {
        int number = Integer.parseInt("12a"); // This will fail
        System.out.println(number);
    }
}

This code produces the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "12a"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:652)
    at java.base/java.lang.Integer.parseInt(Integer.java:770)
    at NumberFormatExceptionExample.main(NumberFormatExceptionExample.java:3)

The stack trace clearly shows where the exception occurred and what input string caused the problem.

How to Handle NumberFormatException

There are several strategies to handle this exception effectively:

1. Basic Try-Catch Approach

public class SafeNumberParsing {
    public static void main(String[] args) {
        String input = "12a";

        try {
            int number = Integer.parseInt(input);
            System.out.println("Parsed number: " + number);
        } catch (NumberFormatException e) {
            System.out.println("Invalid number format: " + input);
            // Log the exception or provide user feedback
        }

        System.out.println("Program continues normally...");
    }
}

Output:

Invalid number format: 12a
Program continues normally...

2. Input Validation Before Parsing

public static boolean isValidInteger(String str) {
    if (str == null || str.trim().isEmpty()) {
        return false;
    }

    try {
        Integer.parseInt(str.trim());
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

// Usage
String userInput = "123";
if (isValidInteger(userInput)) {
    int number = Integer.parseInt(userInput.trim());
    // Process the valid number
} else {
    // Handle invalid input
    System.out.println("Please enter a valid integer");
}

3. Providing Default Values

public static int parseIntWithDefault(String str, int defaultValue) {
    try {
        return Integer.parseInt(str.trim());
    } catch (NumberFormatException | NullPointerException e) {
        return defaultValue;
    }
}

// Usage
int age = parseIntWithDefault(userInput, 0); // Returns 0 if parsing fails

4. Using Regular Expressions for Pre-validation

public static boolean isNumeric(String str) {
    return str != null && str.matches("-?\\d+"); // Matches integers (including negative)
}

if (isNumeric(input)) {
    int number = Integer.parseInt(input);
    // Process number
}

Best Practices for Prevention

  • Always validate user input before attempting to parse it
  • Trim whitespace from strings before parsing: Integer.parseInt(input.trim())
  • Use appropriate parsing methods for your data type (e.g., Double.parseDouble() for decimals)
  • Consider using Scanner class for more robust input parsing from users
  • Implement proper error handling instead of letting exceptions crash your program
  • Log exceptions with meaningful messages for debugging purposes (tools like Rollbar can help automate this process)

Alternative Approaches

Using Scanner for User Input

Java's Scanner class is particularly useful when dealing with user input because it has built-in validation methods. Instead of parsing a string and hoping it works, you can check if the input is actually a number before attempting to read it:

Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");

if (scanner.hasNextInt()) {
    int number = scanner.nextInt();
    System.out.println("You entered: " + number);
} else {
    System.out.println("That's not a valid integer!");
    scanner.next(); // Clear the invalid input
}

The hasNextInt() method returns true only if the next input can be successfully parsed as an integer, eliminating the need for try-catch blocks in this scenario.

Using Apache Commons Lang

If you're already using Apache Commons Lang in your project (a popular utility library), it provides some handy number validation methods that can make your code cleaner:

// Add this dependency to use Apache Commons Lang
import org.apache.commons.lang3.math.NumberUtils;

String input = "123abc";
if (NumberUtils.isCreatable(input)) {
    int number = Integer.parseInt(input);
} else {
    System.out.println("Invalid number format");
}

NumberUtils.isCreatable() is more flexible than writing your own validation—it handles integers, decimals, scientific notation, and various edge cases that might trip up a custom regex solution.

Key Takeaways

  • NumberFormatException occurs when string-to-number conversion fails due to incompatible formats
  • Always handle this exception gracefully rather than letting your program crash
  • Input validation before parsing can prevent many exceptions
  • Consider providing default values or user-friendly error messages
  • The exception message usually contains the problematic input string, making debugging easier

The bottom line? NumberFormatException doesn't have to be a source of frustration. With proper validation and error handling, you can turn potential crashes into opportunities to guide users toward valid input, making your applications more reliable and user-friendly.

But let's be honest—no matter how careful you are, exceptions will slip through in real applications with real users. When exceptions do occur in production, having the right monitoring tools in place makes all the difference...

Track, Analyze and Manage Errors with Rollbar

Rollbar in action

Finding exceptions in your Java code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring, tracking and triaging, making fixing Java errors and exceptions easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape