Java Scanner hasNextBigDecimal() Method

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

Definition

In Java, sc.hasNextBigDecimal() checks whether the next token represents a valid finite decimal number for nextBigDecimal() under the scanner's current locale().

Use it to validate invoice prices, tax rates, or exact decimal amounts before adding them without throwing InputMismatchException.

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

Syntax

Set Locale.US when validating dot-decimal input, then guard reads with hasNextBigDecimal().

Java
Scanner sc = new Scanner("10.25 TBD 4.75").useLocale(Locale.US);
while (sc.hasNext()) {
    if (sc.hasNextBigDecimal()) { /* safe to call sc.nextBigDecimal() */ }
}
CallWhat it does
sc.hasNextBigDecimal()Returns true if the token is a finite decimal in the current locale.
sc.useLocale(Locale.US)Sets dot-decimal syntax regardless of OS default locale.

Unlike hasNextDouble(), hasNextBigDecimal() returns false for "NaN" and "Infinity" because BigDecimal represents only finite numbers.

Library declarations
Java
// java.util.Scanner
public boolean hasNextBigDecimal()

Parameters

ParameterDescription
NonehasNextBigDecimal() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the token is a valid finite decimal in the scanner's locale; false otherwise.
Throws IllegalStateExceptionIf the scanner is closed.

Compare with Similar Methods

Exact decimal lookahead vs floating-point:

MethodHow it differs
nextBigDecimal()Consumes and builds the exact BigDecimal.
hasNextDouble()Also returns true for NaN and Infinity.
hasNextBigInteger()Requires whole numbers with no decimal point.

Java Examples

Example 1 — See Why NaN and Currency Symbols Fail hasNextBigDecimal()

Check "19.99", "NaN", and "$5.00" under Locale.US.

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

public class ScannerHasNextBigDecFinite {
    public static void main(String[] args) {
        String items = "19.99 NaN $5.00";
        try (Scanner sc = new Scanner(items).useLocale(Locale.US)) {
            while (sc.hasNext()) {
                boolean ok = sc.hasNextBigDecimal();
                System.out.println(sc.next() + " -> hasNextBigDecimal? " + ok);
            }
        }
    }
}

How It Works

  1. Plain decimal matches. Under Locale.US, "19.99" passes cleanly.
  2. NaN rejected. Unlike floating-point checks, hasNextBigDecimal() returns false for "NaN".
  3. No currency prefix allowed. "$5.00" fails because $ is not a numeric sign or digit.

Example 2 — Check German Grouped Decimals With Two Locales

Verify "1.234,50" with a Locale.US scanner and a Locale.GERMANY scanner without throwing.

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

public class ScannerHasNextBigDecLocale {
    public static void main(String[] args) {
        try (Scanner us = new Scanner("1.234,50").useLocale(Locale.US);
             Scanner de = new Scanner("1.234,50").useLocale(Locale.GERMANY)) {
            System.out.println("US valid? " + us.hasNextBigDecimal());
            System.out.println("DE valid? " + de.hasNextBigDecimal());
            BigDecimal amount = de.nextBigDecimal();
            System.out.println("Parsed amount: " + amount);
        }
    }
}

How It Works

  1. US rejects dot grouping + comma decimal. hasNextBigDecimal() returns false and leaves "1.234,50" unread.
  2. German locale accepts it. A second scanner with Locale.GERMANY treats the dot as thousands grouping and the comma as the decimal separator.
  3. Scale preserved on read. Calling nextBigDecimal() produces 1234.50.

Example 3 — Accumulate Exact Invoice Amounts and Skip "TBD" Markers

Sum valid decimal entries into a BigDecimal while discarding placeholder text.

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

public class ScannerHasNextBigDecSum {
    public static void main(String[] args) {
        String bill = "10.25 TBD 4.75 N/A 5.00";
        BigDecimal sum = BigDecimal.ZERO;
        try (Scanner sc = new Scanner(bill).useLocale(Locale.US)) {
            while (sc.hasNext()) {
                if (sc.hasNextBigDecimal()) {
                    sum = sum.add(sc.nextBigDecimal());
                } else {
                    System.out.println("Skipped marker: " + sc.next());
                }
            }
        }
        System.out.println("Invoice sum: " + sum);
    }
}

How It Works

  1. Placeholders logged and cleared. "TBD" and "N/A" return false and are consumed via sc.next().
  2. Exact scale-2 sum. Adding 10.25 + 4.75 + 5.00 yields exact 20.00.
  3. Zero rounding error. Using BigDecimal keeps the cents exact without binary floating-point drift.

Frequently Asked Questions

No. Unlike hasNextDouble(), hasNextBigDecimal() returns false for NaN and Infinity.
Because the default locale uses commas for decimals. Call sc.useLocale(Locale.US) for dot-decimal inputs.
Yes. Integers are valid decimals with scale 0, so hasNextBigDecimal() returns true.
No. It only inspects the next token without consuming it.
It throws IllegalStateException.

Next: hasNextBigInteger()

Check whether the next token can be parsed as a BigInteger.

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

12 people found this page helpful