{"record":{"id":"0fcb3a0928649384","repo":"TheAlgorithms/Java","slug":"number-must-be-non-negative-0fcb3a","errorCode":null,"errorMessage":"Number must be non-negative.","messagePattern":"Number must be non-negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java","lineNumber":31,"sourceCode":" */\npublic final class DecimalToAnyUsingStack {\n\n    private DecimalToAnyUsingStack() {\n    }\n\n    private static final char[] DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};\n\n    /**\n     * Convert a decimal number to another radix.\n     *\n     * @param number the number to be converted\n     * @param radix the radix\n     * @return the number represented in the new radix as a String\n     * @throws IllegalArgumentException if number is negative or radix is not between 2 and 16 inclusive\n     */\n    public static String convert(int number, int radix) {\n        if (number < 0) {\n            throw new IllegalArgumentException(\"Number must be non-negative.\");\n        }\n        if (radix < 2 || radix > 16) {\n            throw new IllegalArgumentException(String.format(\"Invalid radix: %d. Radix must be between 2 and 16.\", radix));\n        }\n\n        if (number == 0) {\n            return \"0\";\n        }\n\n        Stack<Character> digitStack = new Stack<>();\n        while (number > 0) {\n            digitStack.push(DIGITS[number % radix]);\n            number /= radix;\n        }\n\n        StringBuilder result = new StringBuilder(digitStack.size());\n        while (!digitStack.isEmpty()) {\n            result.append(digitStack.pop());","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java#L13-L49","documentation":"DecimalToAnyUsingStack.convert(int, int) converts a decimal number to a target radix by repeatedly pushing digits onto a stack. The algorithm only works for non-negative values (it divides and takes remainders), so a negative number is rejected up front with IllegalArgumentException rather than producing a wrong or infinite result.","triggerScenarios":"Calling `convert(number, radix)` with number < 0, e.g. `convert(-10, 2)`.","commonSituations":"Parsing user/CLI input that accepted a leading minus sign; computing a value that underflows to negative; subtracting offsets without guarding against going below zero; feeding signed measurements into a conversion routine.","solutions":["Validate the value is >= 0 before calling, or take the absolute value if sign is irrelevant.","Handle the sign yourself: convert the magnitude and prefix a '-' for the output.","Reject negative input at the input/parse boundary with a clear error."],"exampleFix":"// before\nString s = DecimalToAnyUsingStack.convert(value, 16); // value = -10\n\n// after\nString s = (value < 0)\n    ? \"-\" + DecimalToAnyUsingStack.convert(-value, 16)\n    : DecimalToAnyUsingStack.convert(value, 16);","handlingStrategy":"validation","validationCode":"static String safeConvert(int number, int radix) {\n    if (number < 0) {\n        throw new IllegalArgumentException(\"number must be >= 0, got \" + number);\n    }\n    return DecimalToAnyUsingStack.convert(number, radix);\n}","typeGuard":"static boolean isConvertible(int number) {\n    return number >= 0;\n}","tryCatchPattern":null,"preventionTips":["Validate parsed numeric input for a sign at the parse boundary.","Decide a sign policy (reject vs. magnitude-with-sign) once and reuse it.","Guard subtraction/arithmetic that could go negative before conversion."],"tags":["conversion","radix","validation","illegalargument","number-format"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}