TheAlgorithms/Java · error · IllegalArgumentException

Invalid radix: %d. Radix must be between 2 and 16.

Error message

Invalid radix: %d. Radix must be between 2 and 16.

What it means

DecimalToAnyUsingStack.convert indexes into a fixed DIGITS array of 16 characters ('0'..'F') to map each remainder to a digit glyph, so only radices 2..16 are representable. Any radix below 2 (no positional meaning) or above 16 (no digit symbol available) is rejected with a formatted IllegalArgumentException that echoes the offending radix.

Source

Thrown at src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java:34

    private DecimalToAnyUsingStack() {
    }

    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * Convert a decimal number to another radix.
     *
     * @param number the number to be converted
     * @param radix the radix
     * @return the number represented in the new radix as a String
     * @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive
     */
    public static String convert(int number, int radix) {
        if (number < 0) {
            throw new IllegalArgumentException("Number must be non-negative.");
        }
        if (radix < 2 || radix > 16) {
            throw new IllegalArgumentException(String.format("Invalid radix: %d. Radix must be between 2 and 16.", radix));
        }

        if (number == 0) {
            return "0";
        }

        Stack<Character> digitStack = new Stack<>();
        while (number > 0) {
            digitStack.push(DIGITS[number % radix]);
            number /= radix;
        }

        StringBuilder result = new StringBuilder(digitStack.size());
        while (!digitStack.isEmpty()) {
            result.append(digitStack.pop());
        }

        return result.toString();

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use a radix in the range 2..16 inclusive.
  2. Bounds-check a caller-supplied radix before invoking convert.
  3. For bases > 16, use a different library that supports extended digit alphabets.

Example fix

// before
String s = DecimalToAnyUsingStack.convert(255, 36); // radix > 16

// after
if (radix < 2 || radix > 16) {
    throw new IllegalArgumentException("This class supports radix 2..16 only, got " + radix);
}
String s = DecimalToAnyUsingStack.convert(255, radix);
Defensive patterns

Strategy: validation

Validate before calling

static boolean isValidRadix(int radix) {
    return radix >= 2 && radix <= 16;
}

static String safeConvert(int number, int radix) {
    if (!isValidRadix(radix)) {
        throw new IllegalArgumentException("radix must be 2..16, got " + radix);
    }
    return DecimalToAnyUsingStack.convert(number, radix);
}

Prevention

When it happens

Trigger: Calling `convert(number, radix)` with radix < 2 (e.g. 0, 1) or radix > 16 (e.g. 17, 36). Common mistake: `convert(255, 0)` or trying base-36 which this class does not support.

Common situations: Assuming the class supports arbitrary bases (it does not — only up to 16); passing a radix read from config without bounds-checking; confusing this limited implementation with a full base-conversion utility.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/8ede10171dd69bdf. Report an issue: GitHub.