In Java, sc.hasNextFloat() looks ahead at the next token and returns true if it matches the scanner locale's decimal number syntax for a 32-bit float.
Like hasNextDouble(), it accepts integers, decimals, scientific notation, NaN, and Infinity without consuming input.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.hasNextFloat(...).
Syntax
Use Locale.US for portable dot decimals before checking hasNextFloat().
Java
Scanner sc = new Scanner("1.25 err 3.5").useLocale(Locale.US);
while (sc.hasNext()) {
if (sc.hasNextFloat()) {
float v = sc.nextFloat();
} else {
sc.next();
}
}
Call
What it does
sc.hasNextFloat()
Tests whether the next token can be parsed with nextFloat().
sc.useLocale(Locale.US)
Ensures dot-decimal input works on all locales.
Huge numbers like "1e100" still return true for hasNextFloat() because nextFloat() overflows them to Infinity rather than throwing.
Library declarations
Java
// java.util.Scanner
public boolean hasNextFloat()
Parameters
Parameter
Description
None
hasNextFloat() takes no parameters.
Return Value
Return Value
Description
boolean
true if the token matches valid floating-point syntax in the scanner's locale.