
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
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
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
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
- Related Articles
- Parse a string to a Boolean object in Java
- How to convert a double value to String in Java?
- How to Convert Double to String to Double in Java?
- How to parse a JSON string using Streaming API in Java?
- How to convert a Double array to a String array in java?
- How to parse for words in a string for a specific word in java?
- Convert a String to a double type number in Java
- How to check if a string is a valid keyword in Java?
- How to parse a string to an int in C++?
- How to parse a string from a JavaScript array?
- Convert double to string in Java
- Convert String to Double in Java
- How to parse a string into a nullable int in C#?
- How to parse a string to float or int in python?
- Parse Octal string to create BigInteger in Java
