Java Float parseFloat() MethodThe 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:
Example 1Test it NowOutput: 1. Sum =10076.0 2. Sum =176.0 3. Multiplication =7600.0 4. Division =1.3157895 5. Reminder =24.0 Example 2Test it NowOutput: Enter your Birth of year:2003 Your age = 15.0 Sorry! You are not eligible.Try again after 3.0 years Example 3Test it NowOutput: 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 4Test it NowOutput: Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122) Example 5Test it NowOutput: 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
|