Introduction
In this lab, we will learn how to convert a float value into its equivalent String representation using the Float.toString(float f) method of the java.lang.Float class. We will go through step-by-step instructions to enable us to understand and execute the code for the same.
Create a Java File
Open a text editor of your choice and create a new file named
FloatToString.java.Save the file in the
~/projectdirectory.In the terminal, navigate to the project directory by typing the following command:
cd ~/project
Declare and Initialize Float Variables
Declare two float variables
aandband initialize them with float values of your choice.For example:
float a = 12.345f; float b = -67.89f;
Use toString() Method to Convert Float to String
Use the
Float.toString()method to convert the float variablesaandbto their equivalent String representation.For example:
String stringA = Float.toString(a); String stringB = Float.toString(b);
Print the Converted Strings
Use the
System.out.println()method to print the equivalent String representations of float variablesaandb.For example:
System.out.println("Equivalent String of a is : " + stringA); System.out.println("Equivalent String of b is : " + stringB);
Execute the Code
Save the file.
Open the terminal and navigate to the project directory.
Compile the
FloatToString.javafile by typing the following command:javac FloatToString.javaRun the compiled program by typing the command:
java FloatToString
Provide User Input
- In this step, let's create an example to read user input and convert the entered float value to its equivalent string representation.
- We can use the
Scannerclass to read the input from the user. Add the following lines to theFloatToString.javafile.
import java.util.Scanner;
- Also, add the following code snippet to the main method.
Scanner sc = new Scanner(System.in);
System.out.print("Enter a float value: ");
float userInput = sc.nextFloat();
String stringInput = Float.toString(userInput);
System.out.println("The equivalent String of "+ userInput +" is "+ stringInput);
Execute the Updated Code
Save the file.
Compile the
FloatToString.javafile by typing the following command:javac FloatToString.javaRun the compiled program by typing the command:
java FloatToStringThe program will now prompt the user to input a float value, which will be converted to its equivalent string representation and displayed in the console.
Implement Exception Handling
- It is a good programming practice to handle exceptions in your code. We can handle the exceptions using the try-catch block. Let us modify the code in the main method to include error handling.
- Replace the existing code in the main method with the following.
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a float value: ");
float userInput = sc.nextFloat();
String stringInput = Float.toString(userInput);
System.out.println("The equivalent String of "+ userInput +" is "+ stringInput);
}
catch(Exception e)
{
System.out.println("Invalid input! Please enter a valid float value.");
}
Execute the Updated Code
Save the file.
Compile the
FloatToString.javafile by typing the following command:javac FloatToString.javaRun the compiled program by typing the command:
java FloatToStringThe program will now handle the errors gracefully and prompt the user to enter valid input if an exception is thrown.
Summary
In this lab, we learned how to convert a float value to its equivalent string representation using the Float.toString(float f) method. We went through the step-by-step instructions to execute the code, take user input, handle errors and print the equivalent string representations for float values.



