Home Java Scanner Methods nextBigInteger() Java Scanner nextBigInteger() Method Definition In Java, sc.nextBigInteger() parses the next token into an arbitrary-precision java.math.BigInteger. Unlike int or long, a BigInteger has no fixed bit limit and never overflows.
A second overload, nextBigInteger(int radix), lets you read gigantic binary, octal, or hexadecimal strings directly.
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.nextBigInteger(...).
Syntax Import java.math.BigInteger and call nextBigInteger() (or pass a radix from 2 to 36).
Scanner sc = new Scanner("999999999999999999999999 ff");
BigInteger huge = sc.nextBigInteger();
BigInteger hex = sc.nextBigInteger(16); Call What it does sc.nextBigInteger()Parses an unlimited-length integer using the scanner's current radix. sc.nextBigInteger(16)Parses an arbitrarily long hexadecimal integer. sc.hasNextBigInteger()Checks whether the next token is a valid integer before reading.
Because BigInteger is an object, use methods like .add(...) and .multiply(...) instead of + or *.
Library declarations // java.util.Scanner
public BigInteger nextBigInteger()
public BigInteger nextBigInteger(int radix) Parameters Parameter Description radix (optional)Number base (2 to 36) used to parse digits.
Return Value Return Value Description BigIntegerAn immutable arbitrary-precision integer. Throws InputMismatchException If the token contains invalid characters (such as a decimal point) for the radix. Throws NoSuchElementException If no more tokens are left. Throws IllegalStateException If the scanner has been closed.
Compare with Similar Methods Big integer vs fixed-width integer readers:
Method How it differs hasNextBigInteger()Checks token validity without consuming it. nextLong()Fast primitive, capped at ~9.22e18. nextBigDecimal()Unlimited size plus decimal fractions. new BigInteger(s)Constructs directly from a String.
Java Examples 1. Huge integer arithmetic 2. 128-bit hex token 3. Reject decimals Example 1 — Add 25-Digit Numbers That Overflow long Read numbers far past Long.MAX_VALUE and add them accurately with BigInteger.add().
import java.math.BigInteger;
import java.util.Scanner;
public class ScannerNextBigIntSum {
public static void main(String[] args) {
String data = "1000000000000000000000000 2500000000000000000000005";
try (Scanner sc = new Scanner(data)) {
BigInteger a = sc.nextBigInteger();
BigInteger b = sc.nextBigInteger();
System.out.println("Sum: " + a.add(b));
}
}
} Sum: 3500000000000000000000005 How It Works Beyond 64-bit limits. Both 25-digit numbers would fail nextLong(), yet nextBigInteger() reads them effortlessly.Object arithmetic. Calling a.add(b) computes the exact sum without losing a single trailing digit.Exact decimal output. BigInteger.toString() prints every digit with no E-notation rounding.Example 2 — Read a 128-Bit Hex Key with nextBigInteger(16) Use radix 16 to load a 32-character hexadecimal key into a BigInteger.
import java.math.BigInteger;
import java.util.Scanner;
public class ScannerNextBigIntHex {
public static void main(String[] args) {
String key = "00000000000000010000000000000001";
try (Scanner sc = new Scanner(key)) {
BigInteger val = sc.nextBigInteger(16);
System.out.println("Bit length: " + val.bitLength());
System.out.println("Decimal: " + val);
}
}
} Bit length: 65
Decimal: 18446744073709551617 How It Works Radix 16 accepts 128-bit tokens. The 32 hex digits translate to 2^64 + 1, one larger than an unsigned 64-bit word.Inspect bit size. bitLength() reports 65, proving the value requires more than 64 bits.Exact conversion. Printing val outputs 18446744073709551617 accurately.Example 3 — Catch InputMismatchException When a Decimal Appears Verify that nextBigInteger() rejects decimal numbers like "12.50" while accepting integers.
import java.math.BigInteger;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerNextBigIntReject {
public static void main(String[] args) {
try (Scanner sc = new Scanner("12.50 42")) {
while (sc.hasNext()) {
try {
BigInteger n = sc.nextBigInteger();
System.out.println("Integer: " + n);
} catch (InputMismatchException e) {
System.out.println("Not an integer (" + e.getClass().getSimpleName() + "): " + sc.next());
}
}
}
}
} Not an integer (InputMismatchException): 12.50
Integer: 42 How It Works Decimals do not match. "12.50" contains a decimal point, so nextBigInteger() throws InputMismatchException.Clear bad token. Reading sc.next() inside catch discards "12.50" so the loop does not stall.Valid integer follows. The second token 42 parses cleanly into a BigInteger.Frequently Asked Questions When should I use nextBigInteger() instead of nextLong()? Whenever numbers can exceed 19 digits (Long.MAX_VALUE is ~9.22e18), such as factorials, cryptography keys, or huge counters.
Does nextBigInteger() accept a radix? Yes. Pass a radix from 2 to 36 to nextBigInteger(radix), or configure a default with useRadix().
Why did nextBigInteger() throw InputMismatchException on "10.0"? BigInteger holds whole numbers only. A decimal point makes the token invalid for nextBigInteger(); use nextBigDecimal() instead.
Can I add two BigInteger values with +? No. BigInteger is a class, so call a.add(b), a.subtract(b), or a.multiply(b).
Is there an upper size limit for nextBigInteger()? No fixed bit limit—it grows dynamically up to available memory.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion