Home Java Scanner Methods hasNextBigInteger() Java Scanner hasNextBigInteger() Method 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().
Scanner sc = new Scanner("999999999999999999999 deadbeef");
boolean huge = sc.hasNextBigInteger();
sc.next();
boolean hex = sc.hasNextBigInteger(16); Call What 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.util.Scanner
public boolean hasNextBigInteger()
public boolean hasNextBigInteger(int radix) Parameters Parameter Description radix (optional)Number base from 2 to 36.
Return Value Return Value Description booleantrue if the token parses as an integer in the given radix; false otherwise.Throws IllegalStateException If the scanner has been closed. Throws IllegalArgumentException If radix is out of 2..36.
Compare with Similar Methods Arbitrary-precision integer lookahead vs fixed bounds:
Java Examples 1. Exceeding long range 2. Validate hex keys 3. Reject fractional input 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.
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);
}
}
}
} 9223372036854775807 -> long=true, BigInteger=true
99999999999999999999 -> long=false, BigInteger=true How It Works Exact long cap passes both. 9223372036854775807 fits in 64 bits and returns true for both methods.20 digits breaks long only. 99999999999999999999 fails hasNextLong() cleanly while hasNextBigInteger() remains true.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.
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());
}
}
}
}
} Valid key bits: 96
Invalid hex: 123g How It Works 96-bit hex accepted. The 24 hex digits exceed 64 bits yet pass hasNextBigInteger(16) cleanly.Invalid digit caught. Because g is not a base-16 digit, "123g" returns false.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.
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);
}
}
}
} 500 -> BigInteger=true, BigDecimal=true
12.34 -> BigInteger=false, BigDecimal=true How It Works Whole number satisfies both. 500 works as both a BigInteger and a zero-scale BigDecimal.Decimal point excluded. 12.34 fails hasNextBigInteger() and returns false.Locale set cleanly. Using Locale.US ensures the dot in 12.34 passes hasNextBigDecimal() reliably.Frequently Asked Questions Does hasNextBigInteger() have a maximum digit limit? No fixed digit limit—any syntactically valid whole number token returns true.
Can hasNextBigInteger() check hexadecimal numbers? Yes. Pass 16 as the radix: sc.hasNextBigInteger(16).
Why does hasNextBigInteger() return false on "10.5"? BigInteger holds whole integers only, so decimal points are rejected.
Does hasNextBigInteger() consume any tokens? No. You must call nextBigInteger() to read it or next() to discard a non-matching token.
What happens if the radix passed to hasNextBigInteger(radix) is invalid? It throws IllegalArgumentException for radices below 2 or above 36.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion