Java Scanner nextBigInteger() Method

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

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

Java
Scanner sc = new Scanner("999999999999999999999999 ff");
BigInteger huge = sc.nextBigInteger();
BigInteger hex = sc.nextBigInteger(16);
CallWhat 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
// java.util.Scanner
public BigInteger nextBigInteger()
public BigInteger nextBigInteger(int radix)

Parameters

ParameterDescription
radix (optional)Number base (2 to 36) used to parse digits.

Return Value

Return ValueDescription
BigIntegerAn immutable arbitrary-precision integer.
Throws InputMismatchExceptionIf the token contains invalid characters (such as a decimal point) for the radix.
Throws NoSuchElementExceptionIf no more tokens are left.
Throws IllegalStateExceptionIf the scanner has been closed.

Compare with Similar Methods

Big integer vs fixed-width integer readers:

MethodHow 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

Example 1 — Add 25-Digit Numbers That Overflow long

Read numbers far past Long.MAX_VALUE and add them accurately with BigInteger.add().

Java
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));
        }
    }
}

How It Works

  1. Beyond 64-bit limits. Both 25-digit numbers would fail nextLong(), yet nextBigInteger() reads them effortlessly.
  2. Object arithmetic. Calling a.add(b) computes the exact sum without losing a single trailing digit.
  3. 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.

Java
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);
        }
    }
}

How It Works

  1. Radix 16 accepts 128-bit tokens. The 32 hex digits translate to 2^64 + 1, one larger than an unsigned 64-bit word.
  2. Inspect bit size. bitLength() reports 65, proving the value requires more than 64 bits.
  3. 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.

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

How It Works

  1. Decimals do not match. "12.50" contains a decimal point, so nextBigInteger() throws InputMismatchException.
  2. Clear bad token. Reading sc.next() inside catch discards "12.50" so the loop does not stall.
  3. Valid integer follows. The second token 42 parses cleanly into a BigInteger.

Frequently Asked Questions

Whenever numbers can exceed 19 digits (Long.MAX_VALUE is ~9.22e18), such as factorials, cryptography keys, or huge counters.
Yes. Pass a radix from 2 to 36 to nextBigInteger(radix), or configure a default with useRadix().
BigInteger holds whole numbers only. A decimal point makes the token invalid for nextBigInteger(); use nextBigDecimal() instead.
No. BigInteger is a class, so call a.add(b), a.subtract(b), or a.multiply(b).
No fixed bit limit—it grows dynamically up to available memory.

Next: nextBoolean()

Read the next token as a boolean (true or false).

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

9 people found this page helpful