{"record":{"id":"ae1fa40dfbf678b7","repo":"TheAlgorithms/Java","slug":"decimal-number-cannot-be-negative","errorCode":null,"errorMessage":"Decimal number cannot be negative.","messagePattern":"Decimal number cannot be negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/DecimalToOctal.java","lineNumber":23,"sourceCode":" */\npublic final class DecimalToOctal {\n    private static final int OCTAL_BASE = 8;\n    private static final int INITIAL_OCTAL_VALUE = 0;\n    private static final int INITIAL_PLACE_VALUE = 1;\n\n    private DecimalToOctal() {\n    }\n\n    /**\n     * Converts a decimal number to its octal equivalent.\n     *\n     * @param decimal The decimal number to convert.\n     * @return The octal equivalent as an integer.\n     * @throws IllegalArgumentException if the decimal number is negative.\n     */\n    public static int convertToOctal(int decimal) {\n        if (decimal < 0) {\n            throw new IllegalArgumentException(\"Decimal number cannot be negative.\");\n        }\n\n        int octal = INITIAL_OCTAL_VALUE;\n        int placeValue = INITIAL_PLACE_VALUE;\n\n        while (decimal != 0) {\n            int remainder = decimal % OCTAL_BASE;\n            octal += remainder * placeValue;\n            decimal /= OCTAL_BASE;\n            placeValue *= 10;\n        }\n\n        return octal;\n    }\n}\n","sourceCodeStart":5,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/DecimalToOctal.java#L5-L39","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check that the input is non-negative before calling convertToOctal.","If negative values are expected in your domain, take the absolute value or handle the sign separately before conversion.","Consider using Integer.toOctalString if you need to handle negative numbers, as it produces the unsigned octal representation."],"exampleFix":"// before\nint octal = DecimalToOctal.convertToOctal(decimal);\n\n// after\nif (decimal < 0) {\n    // Option 1: reject\n    throw new IllegalArgumentException(\"Decimal must be non-negative, got: \" + decimal);\n    // Option 2: use unsigned octal\n    // return Integer.toOctalString(decimal);\n}\nint octal = DecimalToOctal.convertToOctal(decimal);","handlingStrategy":"validation","validationCode":"if (decimal < 0) {\n    throw new IllegalArgumentException(\"Decimal must be non-negative, got: \" + decimal);\n}\nint result = DecimalToOctal.convertToOctal(decimal);","typeGuard":"static boolean isNonNegative(int value) {\n    return value >= 0;\n}","tryCatchPattern":"try {\n    int octal = DecimalToOctal.convertToOctal(decimal);\n} catch (IllegalArgumentException e) {\n    // negative input; use unsigned representation instead\n    String octalStr = Integer.toOctalString(decimal);\n}","preventionTips":["If negative inputs are valid in your domain, use Integer.toOctalString which handles unsigned conversion.","Validate at the computation boundary where the sign is determined.","Add precondition checks in your own methods to catch negative values early."],"tags":["number-conversion","input-validation","octal","range-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}