Java Scanner hasNextBigInteger() Method

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

Definition

In Java, sc.hasNextBigInteger() tests if the next token is a valid whole integer of any length, suitable for nextBigInteger(), without consuming input.

Pass a radix between 2 and 36 to check binary, octal, or hexadecimal tokens of arbitrary length.

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

Syntax

Verify oversized integer tokens or hex strings before parsing with nextBigInteger().

Java
Scanner sc = new Scanner("999999999999999999999 deadbeef");
boolean huge = sc.hasNextBigInteger();
sc.next();
boolean hex = sc.hasNextBigInteger(16);
CallWhat it does
sc.hasNextBigInteger()Returns true if the token is a valid integer in current radix.
sc.hasNextBigInteger(16)Checks whether the token is a valid hexadecimal integer of any length.

Because BigInteger has no fixed bit limit, hasNextBigInteger() returns true even for 50-digit numbers where hasNextLong() returns false.

Library declarations
Java
// java.util.Scanner
public boolean hasNextBigInteger()
public boolean hasNextBigInteger(int radix)

Parameters

ParameterDescription
radix (optional)Number base from 2 to 36.

Return Value

Return ValueDescription
booleantrue if the token parses as an integer in the given radix; false otherwise.
Throws IllegalStateExceptionIf the scanner has been closed.
Throws IllegalArgumentExceptionIf radix is out of 2..36.

Compare with Similar Methods

Arbitrary-precision integer lookahead vs fixed bounds:

MethodHow it differs
nextBigInteger()Consumes the verified BigInteger.
hasNextLong()False past Long.MAX_VALUE (~9.22e18).
hasNextBigDecimal()Also accepts fractional decimals like 12.34.

Java Examples

Example 1 — Detect Numbers Too Big for long Without Failing

Test Long.MAX_VALUE next to a 20-digit integer to see hasNextBigInteger() succeed where hasNextLong() fails.

Java
import java.util.Scanner;

public class ScannerHasNextBigIntOverflow {
    public static void main(String[] args) {
        String nums = "9223372036854775807 99999999999999999999";
        try (Scanner sc = new Scanner(nums)) {
            while (sc.hasNext()) {
                boolean fitsLong = sc.hasNextLong();
                boolean fitsBig = sc.hasNextBigInteger();
                System.out.println(sc.next() + " -> long=" + fitsLong + ", BigInteger=" + fitsBig);
            }
        }
    }
}

How It Works

  1. Exact long cap passes both. 9223372036854775807 fits in 64 bits and returns true for both methods.
  2. 20 digits breaks long only. 99999999999999999999 fails hasNextLong() cleanly while hasNextBigInteger() remains true.
  3. Non-consuming inspection. Checking both predicates before sc.next() compares them on the exact same token.

Example 2 — Validate Long Hexadecimal Tokens with hasNextBigInteger(16)

Check a 128-bit hex token and a malformed hex string containing g before reading.

Java
import java.math.BigInteger;
import java.util.Scanner;

public class ScannerHasNextBigIntHex {
    public static void main(String[] args) {
        String keys = "deadbeefcafebabe12345678 123g";
        try (Scanner sc = new Scanner(keys)) {
            while (sc.hasNext()) {
                if (sc.hasNextBigInteger(16)) {
                    BigInteger key = sc.nextBigInteger(16);
                    System.out.println("Valid key bits: " + key.bitLength());
                } else {
                    System.out.println("Invalid hex: " + sc.next());
                }
            }
        }
    }
}

How It Works

  1. 96-bit hex accepted. The 24 hex digits exceed 64 bits yet pass hasNextBigInteger(16) cleanly.
  2. Invalid digit caught. Because g is not a base-16 digit, "123g" returns false.
  3. Consume invalid token. The else branch removes "123g" with sc.next() so the loop finishes.

Example 3 — See Why Decimals Fail hasNextBigInteger()

Contrast hasNextBigInteger() and hasNextBigDecimal() on whole and fractional inputs.

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

public class ScannerHasNextBigIntDecimal {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("500 12.34").useLocale(Locale.US)) {
            while (sc.hasNext()) {
                boolean bigInt = sc.hasNextBigInteger();
                boolean bigDec = sc.hasNextBigDecimal();
                System.out.println(sc.next() + " -> BigInteger=" + bigInt + ", BigDecimal=" + bigDec);
            }
        }
    }
}

How It Works

  1. Whole number satisfies both. 500 works as both a BigInteger and a zero-scale BigDecimal.
  2. Decimal point excluded. 12.34 fails hasNextBigInteger() and returns false.
  3. Locale set cleanly. Using Locale.US ensures the dot in 12.34 passes hasNextBigDecimal() reliably.

Frequently Asked Questions

No fixed digit limit—any syntactically valid whole number token returns true.
Yes. Pass 16 as the radix: sc.hasNextBigInteger(16).
BigInteger holds whole integers only, so decimal points are rejected.
No. You must call nextBigInteger() to read it or next() to discard a non-matching token.
It throws IllegalArgumentException for radices below 2 or above 36.

Next: hasNextBoolean()

Check whether the next token is true or false (ignoring case).

hasNextBoolean() 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