There are instances in Java programming where there arises a need to filter the string values based on a requirement. For instance, extracting the special characters from a string for decoding it, etc. In such a case, the Double “parseDouble()” method comes into effect that evaluates a valid string value and parses it to its equivalent Double value.
This article will elaborate on applying the Double “parseDouble()” method in Java.
What is the Java Double “parseDouble()” Method?
The “parseDouble()” method of the “Double” class gives a new double that is defined to the value represented by the target string. This method gives the identical value as retrieved via the Double class “valueOf()” method.
Syntax
In this syntax, the parameter “givenString” specifies the string that needs to be parsed.
Exceptions Thrown by the Method
This method throws the following two exceptions:
- NullPointerException: It is thrown when the string that needs to be parsed is “null”.
- NumberFormatException: This exception is returned when the string parsed does not comprise a parsable value.
Example 1: Applying the Double “parseDouble()” Method
This example applies the “parseDouble()” method to return the double value represented by the initialized string:
public static void main(String[] args){
String givenString = "75";
double val = Double.parseDouble(givenString);
System.out.println("Double Value -> " + val);
}}
In the above code lines:
- Firstly, initialize the string that needs to be evaluated.
- In the next step, apply the Double “parseDouble()” method to parse the provided string as its argument.
- It is such that it displays the double value represented by the passed string.
Output
In this output, it can be analyzed that the corresponding double value is retrieved accordingly.
Example 2: Facing the “NullPointerException” With the “parseDouble()” Method
In this example, the “NullPointerException” can be faced upon specifying a “null” value as the discussed method i.e., “parseDouble()” argument:
public static void main(String[] args){
String givenString = null;
double val = Double.parseDouble(givenString);
System.out.println("Double Value -> " + val);
}}
According to this code block:
- Initialize a “null” value.
- After that, apply the “parseDouble()” method to return a double value against the passed “null” value.
- This will result in returning the discussed exception as the “null” value can’t be parsed.
Output
This output implies that by specifying the “null” value as the method’s argument, the discussed exception is returned.
Example 3: Facing the “NumberFormatException” With the “parseDouble()” Method
In this particular example, the “NumberFormatException” can be encountered upon passing the string value that cannot be parsed:
public static void main(String[] args){
String givenString = "";
double val = Double.parseDouble(givenString);
System.out.println("Double Value -> " + val);
}}
In this code snippet:
- Specify an empty value as a “String”.
- Now, apply the “parseDouble()” method to parse the defined string comprising an empty value.
- This resultantly returns the discussed limitation.
Output
As analyzed, the stated exception is displayed upon parsing an “empty” string. This particular exception is also raised upon specifying a special character(s) as a string as it cannot be parsed:
Conclusion
The “parseDouble()” method of the “Double” class in Java returns a new double initialized to the value represented by the provided string. This method raises the “NullPointerException” and the “NumberFormatException” upon passing the “null” string or the string that cannot be parsed, respectively. This write-up demonstrated the working of the “parseDouble()” method in Java.