java.lang.Float parseFloat(String s)

Float.parseFloat in java Overview

The Float.parseFloat(String s) java method is used primarily in parsing a String method argument into a Float object. The Float object is a wrapper class for the float primitive data type of java API. Even though the conversion of String to Float object is a basic stuff, I am suggesting that we should have a mastery on it. In my years of java programming, I have encountered so many instances that I have to convert from String object to another data type. The Float.parseFloat method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Float.Make a note that the parseFloat method of Float class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:

Float.parseFloat(method args)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseFloat method non statically.

Java Float.parseFloat method Syntax

public static float parseFloat(String s) throws NumberFormatException

Method Argument

Data Type Parameter Description
String s the float value represented by the string argument.

Method Returns

The parseFloat(String s)  method of Float class returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.

Compatibility

Java 1.2

Discussion

The method parseFloat(String s) just parse the String input s and eventually return a Float object. As you would have noticed, the String method argument should be a number which means it should be consists by our counting numbers. It is understood that the string input should be consisting of decimal numbers only otherwise invoking the Float.parseFloat method would throw a NumberFormatException.Let’s take the following code snippet:

Float fValue= Float.parseFloat("103");
System.out.println(fValue);

The above code result will give an output of 103. As you would have noticed, we just convert the string “103” into a Float object using the static method Float.parseFloat. You might be wondering what if we put a letter or any other characters beside numbers. The method parseFloat would throw NumberFormatException. Like for this example:

System.out.println(Float.parseFloat("A"));

Running the above code would result to:

Exception in thread "main" java.lang.NumberFormatException: For input string: "A"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)
	at com.javatutorialhq.java.examples.Test.main(Test.java:6)


The above exception will be encountered if the input is not a recognizable value by Float class. If we are asking for a user input, it might be good to catch this exception and return a more user-friendly message.

Java Float.parseFloat maximum and minimum value

There is always a limit on what we could parse. You might be wondering how to determine the maximum and minimum value that is allowable by the parseFloat method. Well getting the threshold is quite easy and can be determine by using the two static values available in Float class which is the following:

Float.MAX_VALUE
Float.MIN_VALUE

Let’s take an example that crosses the minimum and maximum value that we could parse using the Float.parseFloat java method:

System.out.println(Float.parseFloat("3.6028235E38"));

Running the above code would result to the following:

Infinity

Java Float parseFloat(String s) Example

Below is a simple java example on the usage of parseFloat(String s) method of Float class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code is used to calculate
 * the area of a rectangle making use of
 * parseFloat(String s) method of Float class 
 * to get the user input. 
 * 
 */

public class FloatParseFloatExample {

	public static void main(String[] args) {
		
		// Ask user input
		System.out.print("Enter height of rectangle:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get height of a rectangle
		String height = scan.nextLine();
		System.out.println("Enter base of rectangle:");
		//use scanner to get base of a rectangle
		String base = scan.nextLine();
		// close the scanner object
		scan.close();		
		// convert the String input to Float
		Float baseFloat = Float.parseFloat(base);
		Float heightFloat = Float.parseFloat(height);
		// calculate the area of rectangle
		Float areaRectangle = baseFloat * heightFloat;
		System.out.println("Area of the rectangle is "+areaRectangle);
		
	}

}

Basically on the above example, we just ask for two values (base and height ) as user input on the console as String format. These values as it is in String format and we cannot use it to calculate the area, thus we transformed these values into Float using the parseFloat(String s) method before doing the mathematical calculation to get the area.

Sample Output

Below is the sample output when you run the above example.

Float parseFloat(String s) Sample Output