Java Float parseFloat Method

JavaJavaBeginner
Practice Now

Introduction

The parseFloat() method is used to parse a string value into its equivalent float value. The parsed value is returned as a float data type. In this lab, you will learn how to use the parseFloat() method in Java programming with the help of step-by-step instructions.

Create a new Java class

Create a new Java file named FloatParseFloat.java in the ~/project directory using the following command:

cd ~/project
touch FloatParseFloat.java

Declare the main method

Add the following code snippet to declare the main method:

public class FloatParseFloat {
    public static void main(String[] args) {
        // Code goes here
    }
}

Parse a string value to float using parseFloat()

Add the following code snippet to parse a string value stringFloat to float data type:

String stringFloat = "34.89";
float floatValue = Float.parseFloat(stringFloat);
System.out.println("Parsed float value: " + floatValue);

In this step, we initialize a string variable stringFloat with a numeric value "34.89". Then, we parse this string value to float data type using the parseFloat() method. Finally, we print the parsed float value to the console using the System.out.println() method.

Parse an invalid string value to float

Add the following code snippet to parse an invalid string value invalidStringFloat to float data type:

String invalidStringFloat = "NumberFormatException";
float invalidFloat = Float.parseFloat(invalidStringFloat);
System.out.println("Parsed float value: " + invalidFloat);

In this step, we initialize a string variable invalidStringFloat with an invalid string value "NumberFormatException". Then, we parse this invalid string value to float data type using the parseFloat() method. Since the string value is not a valid float value, the parseFloat() method throws a NumberFormatException which will crash the program.

Handle NumberFormatException using try-catch block

Add the following code snippet to handle NumberFormatException using a try-catch block:

try {
    String invalidStringFloat = "NumberFormatException";
    float invalidFloat = Float.parseFloat(invalidStringFloat);
    System.out.println("Parsed float value: " + invalidFloat);
} catch (NumberFormatException e) {
    System.out.println("Invalid string value!");
}

In this step, we add a try-catch block to handle the NumberFormatException that is thrown when an invalid string value is parsed to float data type. Inside the try block, we attempt to parse an invalid string value invalidStringFloat to float data type. If a NumberFormatException is thrown due to an invalid string value, the code inside the catch block is executed which prints an error message "Invalid string value!" to the console.

Handle NullPointerException using try-catch block

Add the following code snippet to handle NullPointerException using a try-catch block:

try {
    String nullStringFloat = null;
    float nullFloat = Float.parseFloat(nullStringFloat);
    System.out.println("Parsed float value: " + nullFloat);
} catch (NullPointerException e) {
    System.out.println("Null string value!");
}

In this step, we add a try-catch block to handle the NullPointerException that is thrown when a null value is passed as an argument to the parseFloat() method. Inside the try block, we attempt to parse a null string value nullStringFloat to float data type. If a NullPointerException is thrown due to a null string value, the code inside the catch block is executed which prints an error message "Null string value!" to the console.

Compile and run the program

Compile and run the FloatParseFloat.java program using the following commands:

javac FloatParseFloat.java
java FloatParseFloat

Observe the output

Observe the output of the program in the console. You should see the following output:

Parsed float value: 34.89
Invalid string value!
Null string value!

In this step, we observe the output of the program in the console. The first line of the output shows the parsed float value. The second and third lines of the output show the error messages when an invalid string value and a null value are passed to the parseFloat() method, respectively.

Summary

In this lab, you learned how to use the parseFloat() method to parse a string value to its equivalent float value in Java programming. You also learned how to handle NumberFormatException and NullPointerException using try-catch blocks.

Other Java Tutorials you may like