Java Scanner nextFloat() Method

The nextFloat() method of Java Scanner class is used to scan the next token of the input as a Float. If the translation is successful, the scanner past the input that matched.

Syntax

Following is the declaration of nextFloat() method:

Parameter

This method does not accept any parameter.

Returns

The nextFloat() method returns the Float scanned from the input.

Exceptions

InputMismatchException- It will thrown this Exception if the next token does not match the Float regular expression, or is out of range.

NoSuchElementException- It will thrown this Exception if the input is exhausted.

IllegalStateException- It will thrown this Exception if the innvocation is done after Scanner is closed.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Enter the amount: 263426
Enter the Total Balance: 3434343.454
New Balance is: 3170917.0

Example 2

Output:

Input value : 
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150)
	at java.base/java.util.Scanner.next(Scanner.java:1573)
	at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
	at myPackage.ScannerNextDoubleExample2.main(ScannerNextDoubleExample2.java:9)

Example 3

Output:

Not Found Float: Hello
Not Found Float: World!
Found Float: 12.0
Not Found Float: +
Found Float: 13.43
Not Found Float: =
Found Float: 15.43
Not Found Float: 2.123

Example 4

Output:

Enter the numeric value : 74537453.56959
Float value : 7.4537456E7 
Twice value : 1.49074912E8