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.

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