Java Float intValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Java intValue() method of the Float class to convert a Float object into an integer 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/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117662{{"`Java Float intValue Method`"}} java/classes_objects -.-> lab-117662{{"`Java Float intValue Method`"}} java/class_methods -.-> lab-117662{{"`Java Float intValue Method`"}} java/exceptions -.-> lab-117662{{"`Java Float intValue Method`"}} java/modifiers -.-> lab-117662{{"`Java Float intValue Method`"}} java/oop -.-> lab-117662{{"`Java Float intValue Method`"}} java/packages_api -.-> lab-117662{{"`Java Float intValue Method`"}} java/user_input -.-> lab-117662{{"`Java Float intValue Method`"}} java/wrapper_classes -.-> lab-117662{{"`Java Float intValue Method`"}} java/identifier -.-> lab-117662{{"`Java Float intValue Method`"}} java/arrays -.-> lab-117662{{"`Java Float intValue Method`"}} java/comments -.-> lab-117662{{"`Java Float intValue Method`"}} java/data_types -.-> lab-117662{{"`Java Float intValue Method`"}} java/operators -.-> lab-117662{{"`Java Float intValue Method`"}} java/output -.-> lab-117662{{"`Java Float intValue Method`"}} java/strings -.-> lab-117662{{"`Java Float intValue Method`"}} java/variables -.-> lab-117662{{"`Java Float intValue Method`"}} java/system_methods -.-> lab-117662{{"`Java Float intValue Method`"}} end

Converting User Input

  1. Open the Java file using the following command:
touch ~/project/FloatToInt.java
  1. Replace the existing code with the following code:
import java.util.Scanner;

public class FloatToInt {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        // Getting the user input as float value
        System.out.print("Enter a floating point value: ");
        float f = sc.nextFloat();

        // Creating a Float object from the user input
        Float f1 = f;

        // Converting the Float object to int
        int i = f1.intValue();

        // Displaying the int value
        System.out.println("Float value: " + f1);
        System.out.println("Int value: " + i);
    }
}
  1. Save the file and exit the editor by pressing CTRL+X, then Y, then ENTER.
  2. Compile the code using the following command:
javac ~/project/FloatToInt.java
  1. Execute the code by running the following command:
java FloatToInt
  1. Enter a floating point value when prompted
  2. The code output should be:
Enter a floating point value: 45.6
Float value: 45.6
Int value: 45

Handling Exception

  1. Open the Java file using the following command:
touch ~/project/FloatToInt.java
  1. Replace the existing code with the following code:
import java.util.Scanner;

public class FloatToInt {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        try {
            // Getting the user input as float value
            System.out.print("Enter a floating point value: ");
            float f = sc.nextFloat();

            // Creating a Float object from the user input
            Float f1 = f;

            // Converting the Float object to int
            int i = f1.intValue();

            // Displaying the int value
            System.out.println("Float value: " + f1);
            System.out.println("Int value: " + i);
        } catch (Exception e) {
            System.out.println("Invalid input. Please enter a valid floating point value.");
        }
    }
}
  1. Save the file and exit the editor by pressing CTRL+X, then Y, then ENTER.
  2. Compile the code using the following command:
javac ~/project/FloatToInt.java
  1. Execute the code by running the following command:
java FloatToInt
  1. Try to enter an invalid input like a string or a hexadecimal value.
  2. The code output should be:
Enter a floating point value: abcd
Invalid input. Please enter a valid floating point value.

Summary

In this lab, you have learned how to use the Java intValue() method of the Float class. You have learned how to create a Float object, convert it to an integer value, and display the output. You have also learned how to handle exceptions when the user enters an invalid input.

Other Java Tutorials you may like