TheAlgorithms/Java · error · IllegalArgumentException

Incorrect input: Expecting an octal number (digits 0-7)

Error message

Incorrect input: Expecting an octal number (digits 0-7)

What it means

Thrown by OctalToDecimal.convertOctalToDecimal when any character in the input string falls outside the octal digit range '0' through '7'. The check is a simple char comparison (currentChar < '0' || currentChar > '7'), so characters like '8', '9', letters, spaces, signs, or punctuation all trigger it.

Source

Thrown at src/main/java/com/thealgorithms/conversions/OctalToDecimal.java:33

     * If the input is not a valid octal number (i.e., contains characters other than 0-7),
     * the method throws an IllegalArgumentException.
     *
     * @param inputOctal The octal number as a string
     * @return The decimal equivalent of the octal number
     * @throws IllegalArgumentException if the input is not a valid octal number
     */
    public static int convertOctalToDecimal(String inputOctal) {
        if (inputOctal == null || inputOctal.isEmpty()) {
            throw new IllegalArgumentException("Input cannot be null or empty");
        }

        int decimalValue = 0;

        for (int i = 0; i < inputOctal.length(); i++) {
            char currentChar = inputOctal.charAt(i);

            if (currentChar < '0' || currentChar > '7') {
                throw new IllegalArgumentException("Incorrect input: Expecting an octal number (digits 0-7)");
            }

            int currentDigit = currentChar - '0';
            decimalValue = decimalValue * OCTAL_BASE + currentDigit;
        }

        return decimalValue;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate the input with a regex like ^[0-7]+$ before calling convertOctalToDecimal.
  2. Strip any prefix (e.g., '0o', '0') or whitespace if your data format includes them, then validate.
  3. If the input might be decimal, route it through Integer.parseInt with the appropriate radix instead.

Example fix

// before
int result = OctalToDecimal.convertOctalToDecimal(octalStr); // may contain '8' or '9'

// after
if (!octalStr.matches("^[0-7]+$")) {
    throw new IllegalArgumentException("Input must contain only octal digits 0-7: " + octalStr);
}
int result = OctalToDecimal.convertOctalToDecimal(octalStr);
Defensive patterns

Strategy: validation

Validate before calling

if (!inputOctal.matches("^[0-7]+$")) {
    throw new IllegalArgumentException("Input must contain only octal digits 0-7: " + inputOctal);
}
int result = OctalToDecimal.convertOctalToDecimal(inputOctal);

Type guard

static boolean isValidOctalDigits(String s) {
    return s != null && s.matches("^[0-7]+$");
}

Try / catch

try {
    int result = OctalToDecimal.convertOctalToDecimal(inputOctal);
} catch (IllegalArgumentException e) {
    // contains non-octal characters; route to decimal parser or reject
    logger.warn("Invalid octal digit in: {}", inputOctal);
}

Prevention

When it happens

Trigger: Passing strings containing '8' or '9' (e.g., '89'). Including whitespace, a sign character ('+' or '-'), or a prefix like '0o'. Providing decimal or hexadecimal input instead of octal.

Common situations: File permission strings on some systems contain non-octal characters. User-entered numeric input is misclassified as octal. A data pipeline routes a mixed-format string into the octal converter by mistake.

Related errors


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