TheAlgorithms/Java · error · IllegalArgumentException

Number must be non-negative.

Error message

Number must be non-negative.

What it means

DecimalToAnyUsingStack.convert(int, int) converts a decimal number to a target radix by repeatedly pushing digits onto a stack. The algorithm only works for non-negative values (it divides and takes remainders), so a negative number is rejected up front with IllegalArgumentException rather than producing a wrong or infinite result.

Source

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

 */
public final class DecimalToAnyUsingStack {

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

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate the value is >= 0 before calling, or take the absolute value if sign is irrelevant.
  2. Handle the sign yourself: convert the magnitude and prefix a '-' for the output.
  3. Reject negative input at the input/parse boundary with a clear error.

Example fix

// before
String s = DecimalToAnyUsingStack.convert(value, 16); // value = -10

// after
String s = (value < 0)
    ? "-" + DecimalToAnyUsingStack.convert(-value, 16)
    : DecimalToAnyUsingStack.convert(value, 16);
Defensive patterns

Strategy: validation

Validate before calling

static String safeConvert(int number, int radix) {
    if (number < 0) {
        throw new IllegalArgumentException("number must be >= 0, got " + number);
    }
    return DecimalToAnyUsingStack.convert(number, radix);
}

Type guard

static boolean isConvertible(int number) {
    return number >= 0;
}

Prevention

When it happens

Trigger: Calling `convert(number, radix)` with number < 0, e.g. `convert(-10, 2)`.

Common situations: Parsing user/CLI input that accepted a leading minus sign; computing a value that underflows to negative; subtracting offsets without guarding against going below zero; feeding signed measurements into a conversion routine.

Related errors


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