{"record":{"id":"8ede10171dd69bdf","repo":"TheAlgorithms/Java","slug":"invalid-radix-d-radix-must-be-between-2-and-16","errorCode":null,"errorMessage":"Invalid radix: %d. Radix must be between 2 and 16.","messagePattern":"Invalid radix: (.+?)\\. Radix must be between 2 and 16\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java","lineNumber":34,"sourceCode":"    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());\n        }\n\n        return result.toString();","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/stacks/DecimalToAnyUsingStack.java#L16-L52","documentation":"DecimalToAnyUsingStack.convert indexes into a fixed DIGITS array of 16 characters ('0'..'F') to map each remainder to a digit glyph, so only radices 2..16 are representable. Any radix below 2 (no positional meaning) or above 16 (no digit symbol available) is rejected with a formatted IllegalArgumentException that echoes the offending radix.","triggerScenarios":"Calling `convert(number, radix)` with radix < 2 (e.g. 0, 1) or radix > 16 (e.g. 17, 36). Common mistake: `convert(255, 0)` or trying base-36 which this class does not support.","commonSituations":"Assuming the class supports arbitrary bases (it does not — only up to 16); passing a radix read from config without bounds-checking; confusing this limited implementation with a full base-conversion utility.","solutions":["Use a radix in the range 2..16 inclusive.","Bounds-check a caller-supplied radix before invoking convert.","For bases > 16, use a different library that supports extended digit alphabets."],"exampleFix":"// before\nString s = DecimalToAnyUsingStack.convert(255, 36); // radix > 16\n\n// after\nif (radix < 2 || radix > 16) {\n    throw new IllegalArgumentException(\"This class supports radix 2..16 only, got \" + radix);\n}\nString s = DecimalToAnyUsingStack.convert(255, radix);","handlingStrategy":"validation","validationCode":"static boolean isValidRadix(int radix) {\n    return radix >= 2 && radix <= 16;\n}\n\nstatic String safeConvert(int number, int radix) {\n    if (!isValidRadix(radix)) {\n        throw new IllegalArgumentException(\"radix must be 2..16, got \" + radix);\n    }\n    return DecimalToAnyUsingStack.convert(number, radix);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Bounds-check any user/config-supplied radix before calling convert.","Remember this class caps at base 16; switch libraries for base-36+ needs.","Document the 2..16 limit at your own API surface so callers know."],"tags":["conversion","radix","validation","illegalargument","bounds"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}