Write code to convert Integer to float
In the IntegerFloatValueLab.java
file, write the following Java code to convert an Integer
object to its float equivalent using the floatValue()
method:
import java.util.Scanner;
public class IntegerFloatValueLab {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Take integer input from user
System.out.print("Enter an integer value: ");
int num = sc.nextInt();
// Convert Integer to float
Integer integerObj = num;
float floatNum = integerObj.floatValue();
// Print the float value
System.out.println("Float value of " + num + " is: " + floatNum);
sc.close();
}
}
In this code, we take an integer value as input from the user using the Scanner
class. The input value is then converted to an Integer
object using autoboxing. Finally, we use the floatValue()
method to convert the Integer
object to its float equivalent and print the result.