Java Scanner nextFloat() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

Definition

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
CallWhat 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

ParameterDescription
NonenextFloat() takes no parameters.

Return Value

Return ValueDescription
floatThe parsed 32-bit IEEE 754 floating-point value.
Throws InputMismatchExceptionIf the token is not a valid decimal number for the scanner's locale.
Throws NoSuchElementExceptionIf no more tokens are available.
Throws IllegalStateExceptionIf the scanner is closed.

Compare with Similar Methods

Single precision vs double and BigDecimal:

MethodHow it differs
hasNextFloat()Checks before reading to avoid exceptions.
nextDouble()64-bit precision (~15-17 digits); Java's usual default.
nextBigDecimal()Exact decimal digits with zero rounding error.
Float.parseFloat(s)Converts a String directly without locale rules.

Java Examples

Example 1 — Parse 3D Coordinates into a float Array

Read X, Y, and Z offsets into float variables with Locale.US.

Java
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;

public class ScannerNextFloatCoords {
    public static void main(String[] args) {
        String point = "1.5 2.25 -0.5";
        float[] vec = new float[3];
        try (Scanner sc = new Scanner(point).useLocale(Locale.US)) {
            for (int i = 0; i < vec.length; i++) {
                vec[i] = sc.nextFloat();
            }
        }
        System.out.println("Vector: " + Arrays.toString(vec));
    }
}

How It Works

  1. Exact binary halves. 1.5, 2.25, and -0.5 are exact sums of powers of two, so float keeps them cleanly.
  2. Locale-safe parsing. Setting Locale.US keeps dot-decimal parsing consistent on every machine.
  3. Compact 32-bit array. Printing with Arrays.toString displays all three parsed coordinates.

Example 2 — Compare 32-Bit Float Rounding Against Double

Parse the same 9-digit pi string with nextFloat() and nextDouble() to see single-precision rounding.

Java
import java.util.Locale;
import java.util.Scanner;

public class ScannerNextFloatPrecision {
    public static void main(String[] args) {
        String piText = "3.14159265 3.14159265";
        try (Scanner sc = new Scanner(piText).useLocale(Locale.US)) {
            float f = sc.nextFloat();
            double d = sc.nextDouble();
            System.out.println("nextFloat:  " + f);
            System.out.println("nextDouble: " + d);
        }
    }
}

How It Works

  1. Fewer mantissa bits. A float has only 24 bits of significand, so 3.14159265 rounds to 3.1415927.
  2. Double preserves more digits. Reading the second copy with nextDouble() keeps all 9 digits intact.
  3. Pick precision deliberately. Choose nextDouble() unless memory size or an API specifically requires float.

Example 3 — Skip Non-Numeric Markers While Reading Float Temperatures

Recover from "N/A" markers inside a temperature feed using InputMismatchException.

Java
import java.util.InputMismatchException;
import java.util.Locale;
import java.util.Scanner;

public class ScannerNextFloatSensor {
    public static void main(String[] args) {
        String stream = "21.5 N/A 19.25";
        try (Scanner sc = new Scanner(stream).useLocale(Locale.US)) {
            while (sc.hasNext()) {
                try {
                    float temp = sc.nextFloat();
                    System.out.println("Temp: " + temp + " C");
                } catch (InputMismatchException e) {
                    System.out.println("Missing sensor value: " + sc.next());
                }
            }
        }
    }
}

How It Works

  1. Mismatch leaves N/A intact. When nextFloat() hits "N/A", it throws InputMismatchException without moving past it.
  2. Discard with next(). Inside catch, sc.next() reads and logs "N/A" so the loop proceeds to 19.25.
  3. Clean output. Both numeric readings print with their exact 32-bit decimal representations.

Frequently Asked Questions

nextFloat() returns a 32-bit float (~7 decimal digits of precision), while nextDouble() returns a 64-bit double (~15-17 digits).
A 32-bit float cannot hold 9 digits accurately, so Scanner rounds it to the nearest float value.
Yes. Like nextDouble(), it expects dot or comma decimals depending on sc.locale(). Use useLocale(Locale.US) for dot decimals.
Float overflows to Infinity (printing Infinity) rather than throwing InputMismatchException.
Mostly when working with APIs or arrays explicitly typed as float[]. Otherwise, nextDouble() is preferred in Java.

Next: nextInt()

Read the next token as an int.

nextInt() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful