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.

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