{"record":{"id":"2a085a503e944485","repo":"TheAlgorithms/Java","slug":"incorrect-input-expecting-an-octal-number-digits","errorCode":null,"errorMessage":"Incorrect input: Expecting an octal number (digits 0-7)","messagePattern":"Incorrect input: Expecting an octal number \\(digits 0-7\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/OctalToDecimal.java","lineNumber":33,"sourceCode":"     * If the input is not a valid octal number (i.e., contains characters other than 0-7),\n     * the method throws an IllegalArgumentException.\n     *\n     * @param inputOctal The octal number as a string\n     * @return The decimal equivalent of the octal number\n     * @throws IllegalArgumentException if the input is not a valid octal number\n     */\n    public static int convertOctalToDecimal(String inputOctal) {\n        if (inputOctal == null || inputOctal.isEmpty()) {\n            throw new IllegalArgumentException(\"Input cannot be null or empty\");\n        }\n\n        int decimalValue = 0;\n\n        for (int i = 0; i < inputOctal.length(); i++) {\n            char currentChar = inputOctal.charAt(i);\n\n            if (currentChar < '0' || currentChar > '7') {\n                throw new IllegalArgumentException(\"Incorrect input: Expecting an octal number (digits 0-7)\");\n            }\n\n            int currentDigit = currentChar - '0';\n            decimalValue = decimalValue * OCTAL_BASE + currentDigit;\n        }\n\n        return decimalValue;\n    }\n}\n","sourceCodeStart":15,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java#L15-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the input with a regex like ^[0-7]+$ before calling convertOctalToDecimal.","Strip any prefix (e.g., '0o', '0') or whitespace if your data format includes them, then validate.","If the input might be decimal, route it through Integer.parseInt with the appropriate radix instead."],"exampleFix":"// before\nint result = OctalToDecimal.convertOctalToDecimal(octalStr); // may contain '8' or '9'\n\n// after\nif (!octalStr.matches(\"^[0-7]+$\")) {\n    throw new IllegalArgumentException(\"Input must contain only octal digits 0-7: \" + octalStr);\n}\nint result = OctalToDecimal.convertOctalToDecimal(octalStr);","handlingStrategy":"validation","validationCode":"if (!inputOctal.matches(\"^[0-7]+$\")) {\n    throw new IllegalArgumentException(\"Input must contain only octal digits 0-7: \" + inputOctal);\n}\nint result = OctalToDecimal.convertOctalToDecimal(inputOctal);","typeGuard":"static boolean isValidOctalDigits(String s) {\n    return s != null && s.matches(\"^[0-7]+$\");\n}","tryCatchPattern":"try {\n    int result = OctalToDecimal.convertOctalToDecimal(inputOctal);\n} catch (IllegalArgumentException e) {\n    // contains non-octal characters; route to decimal parser or reject\n    logger.warn(\"Invalid octal digit in: {}\", inputOctal);\n}","preventionTips":["Use a regex like ^[0-7]+$ to validate the entire string in one check.","Strip whitespace and any prefix (e.g., '0o') before validation.","If input format is ambiguous, detect decimal vs octal before choosing a converter."],"tags":["number-conversion","input-validation","octal"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}