TheAlgorithms/Java · error · IllegalArgumentException

Decimal number cannot be negative.

Error message

Decimal number cannot be negative.

What it means

Thrown by DecimalToOctal.convertToOctal when the decimal argument is negative. The method's algorithm iteratively computes octal digits via modulo and division, which only works correctly for non-negative integers; a negative input would produce incorrect digit extraction.

Source

Thrown at src/main/java/com/thealgorithms/conversions/DecimalToOctal.java:23

 */
public final class DecimalToOctal {
    private static final int OCTAL_BASE = 8;
    private static final int INITIAL_OCTAL_VALUE = 0;
    private static final int INITIAL_PLACE_VALUE = 1;

    private DecimalToOctal() {
    }

    /**
     * Converts a decimal number to its octal equivalent.
     *
     * @param decimal The decimal number to convert.
     * @return The octal equivalent as an integer.
     * @throws IllegalArgumentException if the decimal number is negative.
     */
    public static int convertToOctal(int decimal) {
        if (decimal < 0) {
            throw new IllegalArgumentException("Decimal number cannot be negative.");
        }

        int octal = INITIAL_OCTAL_VALUE;
        int placeValue = INITIAL_PLACE_VALUE;

        while (decimal != 0) {
            int remainder = decimal % OCTAL_BASE;
            octal += remainder * placeValue;
            decimal /= OCTAL_BASE;
            placeValue *= 10;
        }

        return octal;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check that the input is non-negative before calling convertToOctal.
  2. If negative values are expected in your domain, take the absolute value or handle the sign separately before conversion.
  3. Consider using Integer.toOctalString if you need to handle negative numbers, as it produces the unsigned octal representation.

Example fix

// before
int octal = DecimalToOctal.convertToOctal(decimal);

// after
if (decimal < 0) {
    // Option 1: reject
    throw new IllegalArgumentException("Decimal must be non-negative, got: " + decimal);
    // Option 2: use unsigned octal
    // return Integer.toOctalString(decimal);
}
int octal = DecimalToOctal.convertToOctal(decimal);
Defensive patterns

Strategy: validation

Validate before calling

if (decimal < 0) {
    throw new IllegalArgumentException("Decimal must be non-negative, got: " + decimal);
}
int result = DecimalToOctal.convertToOctal(decimal);

Type guard

static boolean isNonNegative(int value) {
    return value >= 0;
}

Try / catch

try {
    int octal = DecimalToOctal.convertToOctal(decimal);
} catch (IllegalArgumentException e) {
    // negative input; use unsigned representation instead
    String octalStr = Integer.toOctalString(decimal);
}

Prevention

When it happens

Trigger: Calling convertToOctal with any negative int value (e.g., -1, -100). Passing a value computed from a subtraction or external source that can go negative.

Common situations: Processing a numeric field from a database or API that can legitimately be negative. Handling a delta or difference value that the caller did not expect to be negative. Using convertToOctal on file permission masks or offsets without prior validation.

Related errors


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