You are currently viewing How To Fix NumberFormatException In Java

How To Fix NumberFormatException In Java

1. Introduction

NumberFormatException is an exception you’ll most probably encounter when dealing with numeric values in string format. In this article, you’ll learn how to fix this exception. You’ll also learn some best practices for avoiding the NumberFormatException.

2. What Java Says about NumberFormatException

According to the Javadoc, a NumberFormatException is thrown to indicate an attempt to convert a string to one of the numeric types, but the string does not have the appropriate format. Common scenarios include: reading user inputs, reading from a file, or dealing with an external application.

3. How to Reproduce

This exception occurs when you attempt to convert invalid strings to numeric values. This can be any of the numeric data types: Integer, Double, Float, BigDecimal, etc. We only focus on the Double class in this tutorial.

3.1. Invalid decimal separator

A common source of this exception is when you attempt to parse a number that has an invalid decimal separator.
The following example uses “,” as the decimal separator. The code will throw a NumberFormatException because Java internally uses “.” as the decimal separator.

        String invalidNumber = "1,5";
        double doubleValue = Double.parseDouble(invalidNumber);//NumberFormatException
        System.out.println("Parsed double value: " + doubleValue);

Running the code will produce the following output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1,5"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:651)
    at Scratch.main(scratch.java:12)

One way to fix this issue is to change the input data to use “.” as the decimal separator.

        String validNumber = "1.5";
        double doubleValue = Double.parseDouble(validNumber);//No NumberFormatException
        System.out.println("Parsed double value: " + doubleValue);

Output:

Parsed double value: 1.5

If for any reason you can’t change the input data, then you can use the NumberFormat class with a Locale that supports “,” as the decimal separator.

        String validNumber = "1,5";
        Locale locale = Locale.FRENCH; // Locale that uses comma as decimal separator
        NumberFormat format = NumberFormat.getInstance(locale);
        Number number = format.parse(validNumber); //No NumberFormatException
        double doubleValue = number.doubleValue();
        System.out.println("Parsed double value: " + doubleValue);

Output:

Parsed double value: 1.5

3.2. Empty String or Null Input

This situation typically happens when reading values from a file (CSV file for example). Depending on where the data is coming from (relational database for example), some fields might be empty or with “null” values.

Empty String

        String emptyString = ""; // Empty string
        double doubleValue = Double.parseDouble(emptyString); // NumberFormatException
        System.out.println("Parsed double value: " + doubleValue);

Output:

Exception in thread "main" java.lang.NumberFormatException: empty String
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:651)
    at Scratch.main(scratch.java:7)

Null String

        String nullString = "null"; // "null" string read from a file
        double doubleValue = Double.parseDouble(nullString);// NumberFormatException
        System.out.println("Parsed double value: " + doubleValue);

Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "null"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:651)
    at Scratch.main(scratch.java:7)

How to Fix

One way to handle this is to clean the input data. If you can’t do that, you can write appropriate code to perform null and empty string checks.

class Scratch {
    public static void main(String[] args) throws Exception {
        String nullableString = "null"; // "null" string read from a file
        double doubleValue = isEmpty(nullableString) ? 0 : Double.parseDouble(nullableString);// No NumberFormatException
        System.out.println("Parsed double value: " + doubleValue);
    }
    private static boolean isEmpty(String nullableString){
        return null==nullableString || "null".equals(nullableString) || nullableString.isBlank();
    }
}

We are assigning a default value of zero (0) to empty and null Strings.

Output:

Parsed double value: 0.0

4. Conclusion

In this brief tutorial, you learned about the NumberFormatException and how to fix it.

You can find the complete code of this article here in GitHub

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.