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


Using the parseDouble() method

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 you, it throws a NumberFormatException.

Therefore, to know whether a particular string is parse-able 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 pars able to double.

Example

 Live Demo

import java.util.Scanner;
public class ParsableToDouble {
   public static void main(String args[]) {
      try {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a string value: ");
         String str = sc.next();
         Double doub = Double.parseDouble(str);
         System.out.println("Value of the variable: "+doub);
      }catch (NumberFormatException ex) {
         System.out.println("Given String is not parsable to double");
      }
   }
}

Output

Enter a string value:
2245g
Given String is not parsable to double

Using the valueOf() method

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.

Example

 Live Demo

import java.util.Scanner;
public class ParsableToDouble {
   public static void main(String args[]) {
      try {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a string value: ");
         String str = sc.next();
         Double doub = Double.valueOf(str);
         System.out.println("Value of the variable: "+doub);
      }catch (NumberFormatException ex) {
         System.out.println("Given String is not parsable to double");
      }
   }
}

Output

Enter a string value:
2245g
Given String is not parsable to double

Using the constructor of the Double class

One of the constructor 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 parsable to Double a NumberFormatException will be thrown.

Example

 Live Demo

import java.util.Scanner;
public class ParsableToDouble {
   public static void main(String args[]) {
      try {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a string value: ");
         String str = sc.next();
         Double doub = new Double(str);
         System.out.println("Value of the variable: "+doub);
      }catch (NumberFormatException ex) {
         System.out.println("Given String is not parsable to double");
      }
   }
}

Output

Enter a string value:
2245g
Given String is not parsable to double
raja
Published on 11-Oct-2019 10:11:31
Advertisements