Javatpoint Logo
Javatpoint Logo

Java Float parseFloat() Method

The parseFloat() method of Java Float class returns a new float value that is initialized to the value corresponding to defined String.

This method executes same as valueOf() method of Float class.

Syntax:

Parameters:

s- This is the string to be parsed.

Return Value:

The parseFloat() method returns the float value corresponding to the parameter passed.

Throws:

The parseFloat() method throws:

  • NullPointerException - If the string passed is null.
  • NumberFormatException - If the string passed does not hold a parsable float.

Example 1

Test it Now

Output:

1. Sum =10076.0
2. Sum =176.0
3. Multiplication =7600.0
4. Division =1.3157895
5. Reminder =24.0

Example 2

Test it Now

Output:

Enter your Birth of year:2003
Your age = 15.0
Sorry! You are not eligible.Try again after 3.0 years

Example 3

Test it Now

Output:

Enter 9 digit password 
reema@1234
Exception in thread "main" java.lang.NumberFormatException: For input string: "reema@1234"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)

Here, there is an exception because the string passed does not hold a parsable float. Its

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.NullPointerException
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)

Example 5

Test it Now

Output:

Enter 9 digit password 
reema panda
An exception java.lang.NumberFormatException: For input string: "reema"has occured.
 Plz enter a valid integer

Next TopicJava Float





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA