Java Float to String Conversion

JavaJavaBeginner
Practice Now

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.


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/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") 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/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/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117684{{"`Java Float to String Conversion`"}} java/exceptions -.-> lab-117684{{"`Java Float to String Conversion`"}} java/oop -.-> lab-117684{{"`Java Float to String Conversion`"}} java/packages_api -.-> lab-117684{{"`Java Float to String Conversion`"}} java/user_input -.-> lab-117684{{"`Java Float to String Conversion`"}} java/wrapper_classes -.-> lab-117684{{"`Java Float to String Conversion`"}} java/identifier -.-> lab-117684{{"`Java Float to String Conversion`"}} java/data_types -.-> lab-117684{{"`Java Float to String Conversion`"}} java/operators -.-> lab-117684{{"`Java Float to String Conversion`"}} java/output -.-> lab-117684{{"`Java Float to String Conversion`"}} java/strings -.-> lab-117684{{"`Java Float to String Conversion`"}} java/variables -.-> lab-117684{{"`Java Float to String Conversion`"}} java/object_methods -.-> lab-117684{{"`Java Float to String Conversion`"}} java/string_methods -.-> lab-117684{{"`Java Float to String Conversion`"}} java/system_methods -.-> lab-117684{{"`Java Float to String Conversion`"}} end

Create a Java File

  • Open a text editor of your choice and create a new file named FloatToString.java.

  • Save the file in the ~/project directory.

  • In the terminal, navigate to the project directory by typing the following command:

    cd ~/project

Declare and Initialize Float Variables

  • Declare two float variables a and b and 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 variables a and b to 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 variables a and b.
  • 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.java file by typing the following command:

    javac FloatToString.java
  • Run 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 Scanner class to read the input from the user. Add the following lines to the FloatToString.java file.
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.java file by typing the following command:

    javac FloatToString.java
  • Run the compiled program by typing the command:

    java FloatToString
  • The 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.java file by typing the following command:

    javac FloatToString.java
  • Run the compiled program by typing the command:

    java FloatToString
  • The 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.

Other Java Tutorials you may like