In Java, sc.nextFloat() reads the next token and converts it into a 32-bit single-precision float. It shares the same locale-dependent decimal and grouping syntax as nextDouble().
Use nextFloat() when filling float[] buffers for graphics, audio, or lightweight sensor arrays where 32-bit storage is required.
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.nextFloat(...).
Syntax
Set Locale.US for portable dot decimals, then call nextFloat().
Java
Scanner sc = new Scanner("3.25 -0.5").useLocale(Locale.US);
float x = sc.nextFloat(); // 3.25f
float y = sc.nextFloat(); // -0.5f
Call
What it does
sc.useLocale(Locale.US).nextFloat()
Reads a 32-bit float using dot decimals.
sc.hasNextFloat()
Tests whether the next token parses as a float.
A float keeps about 7 significant decimal digits compared to ~15-17 for a double. Very long inputs like "3.14159265" are rounded to the nearest 32-bit float (3.1415927).
Library declarations
Java
// java.util.Scanner
public float nextFloat()
Parameters
Parameter
Description
None
nextFloat() takes no parameters.
Return Value
Return Value
Description
float
The parsed 32-bit IEEE 754 floating-point value.
Throws InputMismatchException
If the token is not a valid decimal number for the scanner's locale.