Java Scanner hasNextFloat() Method

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

Definition

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();
    }
}
CallWhat 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

ParameterDescription
NonehasNextFloat() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the token matches valid floating-point syntax in the scanner's locale.
Throws IllegalStateExceptionIf the scanner has been closed.

Compare with Similar Methods

Comparing float lookahead with double and int:

MethodHow it differs
nextFloat()Consumes and converts the 32-bit float.
hasNextDouble()64-bit twin with identical token syntax.
hasNextInt()Rejects decimal points like .5.

Java Examples

Example 1 — Extract Valid Float Measurements from Noisy Readings

Read valid floats from a line containing error markers like "err" using Locale.US.

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

public class ScannerHasNextFloatFilter {
    public static void main(String[] args) {
        String feed = "1.25 err 3.50 bad -0.5";
        try (Scanner sc = new Scanner(feed).useLocale(Locale.US)) {
            while (sc.hasNext()) {
                if (sc.hasNextFloat()) {
                    System.out.println("Reading: " + sc.nextFloat());
                } else {
                    System.out.println("Ignored: " + sc.next());
                }
            }
        }
    }
}

How It Works

  1. Mixed stream guarded. hasNextFloat() separates numbers from "err" and "bad" without throwing.
  2. Trailing zeros simplify. "3.50" parses into float 3.5f and prints as 3.5.
  3. Consume every word once. The else branch calls sc.next() so invalid labels do not stall the scan.

Example 2 — Test Dot vs Comma Decimals Across US and French Locales

Peek at "2.5" and "2,5" while switching between Locale.US and Locale.FRANCE.

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

public class ScannerHasNextFloatLocale {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("2.5 2,5").useLocale(Locale.US)) {
            System.out.println("US 2.5? " + sc.hasNextFloat());
            sc.next();
            System.out.println("US 2,5? " + sc.hasNextFloat());
            sc.useLocale(Locale.FRANCE);
            System.out.println("FR 2,5? " + sc.hasNextFloat() + " -> " + sc.nextFloat());
        }
    }
}

How It Works

  1. US expects dots. "2.5" returns true, whereas "2,5" returns false under Locale.US.
  2. Switch to France for commas. Calling useLocale(Locale.FRANCE) flips the decimal separator rule to comma.
  3. Token remained intact. Because the failed check did not consume "2,5", nextFloat() reads 2.5 right after switching.

Example 3 — Distinguish Integer-Only Tokens from Fractional Floats

Check both hasNextInt() and hasNextFloat() on "42 42.5 NaN".

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

public class ScannerHasNextFloatVsInt {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("42 42.5 NaN").useLocale(Locale.US)) {
            while (sc.hasNext()) {
                boolean isInt = sc.hasNextInt();
                boolean isFlt = sc.hasNextFloat();
                System.out.println(sc.next() + " -> int=" + isInt + ", float=" + isFlt);
            }
        }
    }
}

How It Works

  1. Whole number fits both. "42" satisfies both hasNextInt() and hasNextFloat().
  2. Fraction restricts to float. "42.5" fails the integer check while passing hasNextFloat().
  3. NaN passes float check. Even "NaN" evaluates to true for hasNextFloat().

Frequently Asked Questions

No. Scanner accepts it as valid floating-point syntax, and nextFloat() returns Infinity.
Locale.US uses a dot for decimals, not a comma. Switch to Locale.GERMANY or Locale.FRANCE for comma decimals.
Yes, integers parse cleanly into float 5.0f.
No, it only peeks at the next token.
It throws IllegalStateException.

Next: hasNextInt()

Check whether the next token fits in a signed int.

hasNextInt() 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.

8 people found this page helpful