Java Program to Convert String to Float Value

 In Java, converting a string to a float is a common task especially when dealing with numerical data input as strings. This article discusses different methods to convert a string to a float value in Java focusing on techniques that handle potential exceptions and ensure accurate conversions.

Methods to Convert String to Float Value

There are multiple ways to convert a string to a float in Java. We will explore two methods: 

  1. using Float.parseFloat() method
  2. using Float.valueOf() method

Method 1: Using Float.parseFloat()

The Float.parseFloat() method is a built-in method that converts a string to a float. It throws a NumberFormatException if the string does not contain a parsable float.

Example 1: Successful Conversion

public class Main {

public static void main(String[] args) {

// Input string

String str = "123.45";

// Convert string to float

float val = Float.parseFloat(str);

// Print the float value

System.out.println("String is converted to float: " + val);

}

}

Output:

String is converted to float: 123.45

Example 2: Unsuccessful Conversion with Exception Handling

public class Main {

public static void main(String[] args) {

// Input strings

String[] strArray = { "123.45", "ABC", null, "" };

// Convert each string to float and handle exceptions

for (String str : strArray) {

try {

float val = Float.parseFloat(str);

System.out.println("String '" + str + "' is converted to float: " + val);

} catch (NumberFormatException | NullPointerException e) {

System.out.println("String '" + str + "' cannot be converted to float: " + e.getMessage());

}

}

}

}

Output:

String '123.45' is converted to float: 123.45

String 'ABC' cannot be converted to float: For input string: "ABC"

String 'null' cannot be converted to float: null

String '' cannot be converted to float: empty String

Method 2: Using Float.valueOf()

The Float.valueOf() method converts a string to a Float object which can then be converted to a primitive float using the floatValue() method. This method is also capable of handling exceptions in a similar manner to parseFloat().

Example: Conversion Using Float.valueOf()

public class Main {

public static void main(String[] args) {

// Input strings

String[] strArray = { "456.78", "XYZ", null, " " };

// Convert each string to float and handle exceptions

for (String str : strArray) {

try {

Float floatObj = Float.valueOf(str);

float val = floatObj.floatValue();

System.out.println("String '" + str + "' is converted to float: " + val);

} catch (NumberFormatException | NullPointerException e) {

System.out.println("String '" + str + "' cannot be converted to float: " + e.getMessage());

}

}

}

}

Output:

String '456.78' is converted to float: 456.78

String 'XYZ' cannot be converted to float: For input string: "XYZ"

String 'null' cannot be converted to float: null

String ' ' cannot be converted to float: For input string: " "

Conclusion

When converting strings to float values in Java it is important to handle potential exceptions such as the NumberFormatException and NullPointerException. The Float.parseFloat() and Float.valueOf() methods both provide reliable ways to perform this conversion.

FAQs

Q1. What is the difference between parseFloat() and valueOf()?

The parseFloat() returns a primitive float whereas valueOf() returns a Float object which can be converted to a primitive float using the floatValue() method.

Q2. Can parseFloat() handle null strings?

No, parseFloat() throws a NullPointerException if the input string is null.

Q3. Is there any performance difference between parseFloat() and valueOf()?

The performance difference is negligible in most cases. parseFloat() may be slightly faster as it directly returns a primitive float.

Q4. What should I do if the string contains spaces or non-numeric characters?

Ensure that the string is properly formatted and trimmed before attempting to convert it to a float. Use exception handling to manage any invalid input.

Post a Comment

Previous Post Next Post