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
Call
What 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
Parameter
Description
radix
Number base from Character.MIN_RADIX (2) to Character.MAX_RADIX (36).
Return Value
Return Value
Description
Scanner
The same scanner instance, allowing chaining.
Throws IllegalArgumentException
If radix < 2 or radix > 36.
Throws
Nothing. It does not check whether the scanner is closed.