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/ObjectOrientedandAdvancedConceptsGroup(["Object-Oriented and Advanced Concepts"]) java(("Java")) -.-> java/BasicSyntaxGroup(["Basic Syntax"]) java/BasicSyntaxGroup -.-> java/type_casting("Type Casting") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("User Input") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("Exceptions") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("Wrapper Classes") subgraph Lab Skills java/type_casting -.-> lab-117662{{"Java Float intValue Method"}} java/user_input -.-> lab-117662{{"Java Float intValue Method"}} java/exceptions -.-> lab-117662{{"Java Float intValue Method"}} java/wrapper_classes -.-> 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.