NumberFormatException

java.lang.NumberFormatException: For input string: "abc"

Quick Answer

You are parsing a string that is not a valid number. Validate the input string before parsing or handle the exception.

Why This Happens

Integer.parseInt(), Double.parseDouble(), and similar methods throw NumberFormatException when the input string does not represent a valid number. This happens with non-numeric characters, empty strings, extra whitespace, or numbers with wrong format.

The Problem

String input = "12.5";
int value = Integer.parseInt(input); // NumberFormatException

The Fix

String input = "12.5";
try {
    double value = Double.parseDouble(input.trim());
    int intValue = (int) value;
} catch (NumberFormatException e) {
    System.err.println("Invalid number: " + input);
}

Step-by-Step Fix

  1. 1

    Identify the invalid input

    Read the exception message to see the exact string that failed to parse.

  2. 2

    Check the parsing method

    Ensure you are using the right parsing method for the number format (parseInt for integers, parseDouble for decimals).

  3. 3

    Validate or handle gracefully

    Trim whitespace, validate the format with a regex, or wrap in try-catch and provide a default value or error message.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free