Java Double parseDouble Method

JavaJavaBeginner
Practice Now

Introduction

Java parseDouble() method is used to convert string values to their equivalent double values. This is an important method when it comes to data type conversion. In this lab, we will learn how to use the parseDouble() method and how it converts a string value to its equivalent double value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/classes_objects -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/class_methods -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/exceptions -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/modifiers -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/oop -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/packages_api -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/user_input -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/wrapper_classes -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/identifier -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/arrays -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/comments -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/data_types -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/operators -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/output -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/strings -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/variables -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/string_methods -.-> lab-117627{{"`Java Double parseDouble Method`"}} java/system_methods -.-> lab-117627{{"`Java Double parseDouble Method`"}} end

Implement the parseDouble() method syntax

Implement the following syntax of parseDouble() method in your Java program:

public static double parseDouble(String s) throws NumberFormatException

Use the parseDouble() method to convert a string value to a double value

In this step, we will convert a string value to its equivalent double value using the parseDouble() method.

Code block:

String str = "8.97";
double num = Double.parseDouble(str);
System.out.println("Double value of "+str+" is "+num);

This will output:

Double value of 8.97 is 8.97

In the code block above, we created a string variable str and initialized it with the value "8.97". Then, we called the parseDouble() method, passing the string variable as an argument, and assigned the returned double value to the double variable num. Finally, we printed the double variable.

Catching the possible NumberFormatException

In this step, we will catch the NumberFormatException to avoid unexpected issues.

Code block:

try {
   String str = "not a number"; // exception occurs here
   double num = Double.parseDouble(str);
   System.out.println("Double value of "+str+" is "+num);
} catch(NumberFormatException e) {
   System.out.println("Error: "+e.getMessage()+" is not a valid double");
}

This will output:

Error: For input string: "not a number" is not a valid double

In the code block above, the Double.parseDouble() method is called with a string "not a number", which is not a valid double value. Since the parseDouble() method throws NumberFormatException, we used a try-catch block to catch any exceptions that might occur in the program.

Accepting user input

In this step, we will accept user input using the Scanner class and parse it to a double value using the parseDouble() method.

Code block:

Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
String str = input.next();
double num = Double.parseDouble(str);
System.out.println("Double value of "+str+" is "+num);

This will output:

Enter a number: 9.56
Double value of 9.56 is 9.56

In the code block above, we created an instance of the Scanner class to accept user input. We used the next() method to read user input as a string value, then we passed it to the parseDouble() method and assigned the returned value to the double variable num. Finally, we printed the double variable.

Handling null values

In this step, we will handle the null values passed to the parseDouble() method.

Code block:

String str = null;
try {
   double num = Double.parseDouble(str); // exception occurs here
   System.out.println(num);
} catch (NullPointerException e) {
   System.out.println("Error: "+e.getMessage()+" - It's not possible to convert null to double value");
}

This will output:

Error: null - It's not possible to convert null to double value

In the code block above, we assigned a null value to the string variable str. Then, we called the parseDouble() method and passed the null value as an argument, which throws a NullPointerException. We used a try-catch block to catch the NullPointerException and printed a custom message.

Review the complete code

At this point, our code should look like this:

import java.util.Scanner;

public class JavaDoubleParseDoubleMethod {
   public static void main(String[] args) {
      // Example 1
      String str = "8.97";
      double num = Double.parseDouble(str);
      System.out.println("Double value of "+str+" is "+num);

      // Example 2
      try {
         String str2 = "not a number";
         double num2 = Double.parseDouble(str2);
         System.out.println("Double value of "+str2+" is "+num2);
      } catch(NumberFormatException e) {
         System.out.println("Error: "+e.getMessage()+" is not a valid double");
      }

      // Example 3
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a number: ");
      String str3 = input.next();
      double num3 = Double.parseDouble(str3);
      System.out.println("Double value of "+str3+" is "+num3);

      // Example 4
      String str4 = null;
      try {
         double num4 = Double.parseDouble(str4);
         System.out.println(num4);
      } catch (NullPointerException e) {
         System.out.println("Error: "+e.getMessage()+" - It's not possible to convert null to double value");
      }
   }
}

Compile and run the Java program

To compile and run the Java program, execute the following commands:

cd ~/project
javac JavaDoubleParseDoubleMethod.java
java JavaDoubleParseDoubleMethod

This will execute the Java program and output the following:

Double value of 8.97 is 8.97
Error: For input string: "not a number" is not a valid double
Enter a number: 9.56
Double value of 9.56 is 9.56
Error: null - It's not possible to convert null to double value

Summary

In this lab, we learned how to use the parseDouble() method to convert string values to their equivalent double values. We also learned how to handle exceptions and handle null values in the method. This method is very useful when working with string values and numerical calculations in Java.

Other Java Tutorials you may like