TheAlgorithms/Java · error · IllegalArgumentException
Incorrect octal digit: {}
Error message
Incorrect octal digit: {} What it means
Thrown by OctalToHexadecimal.octalToDecimal when any character in the input string is outside the octal digit range '0'–'7'. The check uses char comparison (currentChar < '0' || currentChar > '7'), so characters '8', '9', letters, whitespace, signs, or special characters all trigger it. The offending character is included in the error message.
Source
Thrown at src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java:32
}
/**
* Converts an Octal number (as a string) to its Decimal equivalent.
*
* @param octalNumber The Octal number as a string
* @return The Decimal equivalent of the Octal number
* @throws IllegalArgumentException if the input contains invalid octal digits
*/
public static int octalToDecimal(String octalNumber) {
if (octalNumber == null || octalNumber.isEmpty()) {
throw new IllegalArgumentException("Input cannot be null or empty");
}
int decimalValue = 0;
for (int i = 0; i < octalNumber.length(); i++) {
char currentChar = octalNumber.charAt(i);
if (currentChar < '0' || currentChar > '7') {
throw new IllegalArgumentException("Incorrect octal digit: " + currentChar);
}
int currentDigit = currentChar - '0';
decimalValue = decimalValue * OCTAL_BASE + currentDigit;
}
return decimalValue;
}
/**
* Converts a Decimal number to its Hexadecimal equivalent.
*
* @param decimalNumber The Decimal number
* @return The Hexadecimal equivalent of the Decimal number
*/
public static String decimalToHexadecimal(int decimalNumber) {
if (decimalNumber == 0) {
return "0";
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate the input with a regex like ^[0-7]+$ before calling octalToDecimal.
- Strip any prefixes or whitespace, then re-validate.
- If the input format is ambiguous, detect it first and route to the correct converter.
Example fix
// before
int decimal = OctalToHexadecimal.octalToDecimal(octalStr);
// after
if (!octalStr.matches("^[0-7]+$")) {
throw new IllegalArgumentException("Input must contain only octal digits 0-7: " + octalStr);
}
int decimal = OctalToHexadecimal.octalToDecimal(octalStr); Defensive patterns
Strategy: validation
Validate before calling
if (!octalNumber.matches("^[0-7]+$")) {
throw new IllegalArgumentException("Input must contain only octal digits 0-7: " + octalNumber);
}
int result = OctalToHexadecimal.octalToDecimal(octalNumber); Type guard
static boolean isValidOctalDigits(String s) {
return s != null && s.matches("^[0-7]+$");
} Try / catch
try {
int result = OctalToHexadecimal.octalToDecimal(octalNumber);
} catch (IllegalArgumentException e) {
// non-octal character present; route to correct parser or reject
logger.warn("Invalid octal digit in: {}", octalNumber);
} Prevention
- Validate the full string with ^[0-7]+$ before conversion.
- Strip prefixes and whitespace before validation.
- Differentiate between octal, decimal, and hex inputs at the data routing stage.
When it happens
Trigger: Passing a string containing '8', '9', letters, whitespace, or any non-octal character. Providing input formatted with a prefix like '0o17'. Supplying a decimal or hex string to the octal converter by mistake.
Common situations: A data pipeline routes mixed-format numeric strings to the octal converter. User input is not validated before conversion. File permission representations contain unexpected characters.
Related errors
- Decimal number cannot be negative.
- Input cannot be null or empty
- Incorrect input: Expecting an octal number (digits 0-7)
- Input cannot be null or empty
- Base must be between 2 and 36
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/cb4951d6d99bfe89.
Report an issue: GitHub.