How to check that a string is parse-able to a double in java?



Let's learn how to check if a string is parseable to a double in Java. We have multiple ways to do this. Some of them are:

Using Double.parseDouble()

The parseDouble() method of the java.lang.Double class accepts a String value, parses it, and returns the double value of the given String.

If you pass a null value to this method, it throws a NullPointerException and if this method is not able to parse the given string into a double value, it throws a NumberFormatException.

Therefore, to know whether a particular string is parseable to double or not, pass it to the parseDouble method and wrap this line with try-catch block. If an exception occurs, this indicates that the given String is not parsable to a double.

Let's look at the steps:

  • Use the try-catch block.
  • In the try block, call the Double.parseDouble() method and pass the string to it.
  • If the string is parseable to a double, it will return the double value.
  • Otherwise, it will throw a NumberFormatException.

Example

Following is the Java program to check if a string is parseable to a double using the Double.parseDouble() method:

public class CheckIfParsableDouble{
   public static void main(String[] args){
      try{
        String str = "123.45";
        double d = Double.parseDouble(str);
        System.out.println("The string is parse-able to double: " + d);
      }catch(NumberFormatException e){
        System.out.println("The string is not parse-able to double.");
      }
   }
}

Output

Following is the output of the above code:

The string is parse-able to double: 123.45

Using Double.valueOf()

Similarly, the valueOf() method of the Double class (also) accepts a String value as a parameter, trims the excess spaces and returns the double value represented by the string. If the value given is not parsable to double this method throws a NumberFormatException.

Follow the same steps as above only replace the parseDouble() method with the valueOf() method.

Example

Following is the Java program to check if a string is parseable to double using the Double.valueOf() method:

public class CheckIfParsableDouble{
   public static void main(String[] args){
      try{
        String str = "123.45";
        double d = Double.valueOf(str);
        System.out.println("The string is parse-able to double: " + d);
      }catch(NumberFormatException e){
        System.out.println("The string is not parse-able to double.");
      }
   }
}

Output

Following is the output of the above code:

The string is parse-able to double: 123.45

Using the constructor of the Double class

One of the constructors of the Double class accepts a String as a parameter and constructs an (Double) object that wraps the given value. If the string passed to this constructor is not parseable to a Double, a NumberFormatException will be thrown.

The following are the steps:

  • Use the try-catch block.
  • In the try block, create a new object of the Double class and pass the string to it.
  • If the string is parseable to a double, it will return the double value.
  • Otherwise, it will throw a NumberFormatException.

Example

Following is the Java program to check if a string is parseable to a double using the Double constructor:

public class CheckIfParsableDouble{
   public static void main(String[] args){
      try{
        String str = "123.4g";
        Double d = new Double(str);
        System.out.println("The string is parse-able to double: " + d);
      }catch(NumberFormatException e){
        System.out.println("The string is not parse-able to double.");
      }
   }
}

Output

Following is the output of the above code:

The string is not parse-able to double.
Updated on: 2025-09-01T14:10:42+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements