Java Scanner useRadix() Method

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

Definition

In Java, sc.useRadix(radix) changes the scanner's default number base for integer parsing and returns the Scanner itself so calls can be chained.

Valid bases range from Character.MIN_RADIX (2) to Character.MAX_RADIX (36). Any value outside 2..36 throws IllegalArgumentException.

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

Syntax

Chain useRadix(radix) to parse binary, octal, hex, or base-36 numbers without repeating the base on every call.

Java
Scanner sc = new Scanner("1010 1111").useRadix(2);
int a = sc.nextInt();   // 10
int b = sc.nextInt();   // 15
CallWhat it does
sc.useRadix(2)Sets binary mode for future nextInt() / hasNextInt() calls.
sc.useRadix(8)Sets octal mode (for inputs like Unix file permissions 755).
sc.useRadix(16)Sets hexadecimal mode (digits 0-9 and a-f).
sc.useRadix(36)Uses digits 0-9 and letters a-z (max valid radix).

In bases above 10, letters are not case-sensitive ("ff" and "FF" both mean 255). Do not prefix hex values with 0x—"0xff" throws InputMismatchException because 'x' is invalid in base 16.

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

Parameters

ParameterDescription
radixNumber base from Character.MIN_RADIX (2) to Character.MAX_RADIX (36).

Return Value

Return ValueDescription
ScannerThe same scanner instance, allowing chaining.
Throws IllegalArgumentExceptionIf radix < 2 or radix > 36.
ThrowsNothing. It does not check whether the scanner is closed.

Compare with Similar Methods

Changing radix vs one-off calls:

MethodHow it differs
radix()Returns the currently active default radix.
nextInt(radix)Uses a custom radix for one read without altering scanner state.
reset()Resets radix back to 10 along with delimiter and locale.

Java Examples

Example 1 — Read a Sequence of Binary Numbers Using useRadix(2)

Chain useRadix(2) so every iteration of hasNextInt() and nextInt() reads binary automatically.

Java
import java.util.Scanner;

public class ScannerUseRadixBinary {
    public static void main(String[] args) {
        String bits = "0001 1010 1111";
        try (Scanner sc = new Scanner(bits).useRadix(2)) {
            while (sc.hasNextInt()) {
                System.out.println("Decimal value: " + sc.nextInt());
            }
        }
    }
}

How It Works

  1. Both lookahead and read inherit radix 2. hasNextInt() and nextInt() automatically validate and parse binary digits.
  2. Clean binary conversion. 0001, 1010, and 1111 convert to 1, 10, and 15.
  3. No per-call argument clutter. Configuring useRadix(2) once keeps the loop concise.

Example 2 — Switch Between Octal (8), Hex (16), and Base 36

Parse an octal Unix permission, a hexadecimal color byte, and a base-36 token up to 36.

Java
import java.util.Scanner;

public class ScannerUseRadixBases {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("755 ff z")) {
            sc.useRadix(8);
            int mode = sc.nextInt();
            sc.useRadix(16);
            int alpha = sc.nextInt();
            sc.useRadix(36);
            int maxDigit = sc.nextInt();
            System.out.println("Octal 755 -> " + mode);
            System.out.println("Hex ff    -> " + alpha);
            System.out.println("Base36 z  -> " + maxDigit);
        }
    }
}

How It Works

  1. Octal permissions. 7*64 + 5*8 + 5 evaluates to 493 in base 8.
  2. Hexadecimal byte. Switching to base 16 turns "ff" into 255.
  3. Base 36 ceiling. Using Character.MAX_RADIX (36) gives z its maximum digit value 35.

Example 3 — Catch IllegalArgumentException for Radices Outside 2..36

Verify that useRadix(1) and useRadix(37) fail with IllegalArgumentException while leaving the previous radix intact.

Java
import java.util.Scanner;

public class ScannerUseRadixInvalid {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("42")) {
            for (int r : new int[] { 1, 37 }) {
                try {
                    sc.useRadix(r);
                } catch (IllegalArgumentException e) {
                    System.out.println("Radix " + r + " failed: " + e.getClass().getSimpleName());
                }
            }
            System.out.println("Radix stayed: " + sc.radix() + ", value=" + sc.nextInt());
        }
    }
}

How It Works

  1. Bounds enforced by Character limits. Anything below 2 or above 36 triggers IllegalArgumentException immediately.
  2. Original radix preserved. Failed useRadix() calls leave the scanner's radix unchanged at 10.
  3. Input unread. Calling nextInt() afterwards parses 42 cleanly.

Frequently Asked Questions

From 2 (Character.MIN_RADIX) to 36 (Character.MAX_RADIX). Anything else throws IllegalArgumentException.
Scanner integer tokens must not include the Java 0x prefix; pass "ff" instead.
No. Decimal numbers are always parsed in base 10.
Yes. "ff" and "FF" both parse as 255.
Call sc.useRadix(10), or call sc.reset() to restore delimiter, locale, and radix together.

Back to the method index

See all 36 Scanner method guides grouped by what they do.

Method index →

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.

15 people found this page helpful