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.


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/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-117680{{"`Java Float parseFloat Method`"}} java/classes_objects -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/class_methods -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/exceptions -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/modifiers -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/oop -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/wrapper_classes -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/identifier -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/arrays -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/comments -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/data_types -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/operators -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/output -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/strings -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/variables -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/string_methods -.-> lab-117680{{"`Java Float parseFloat Method`"}} java/system_methods -.-> lab-117680{{"`Java Float parseFloat Method`"}} end

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