{"record":{"id":"e78925aa385cf61c","repo":"TheAlgorithms/Java","slug":"for-input-string","errorCode":null,"errorMessage":"For input string: {}","messagePattern":"For input string: (.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java","lineNumber":28,"sourceCode":"    private AnyBaseToDecimal() {\n    }\n\n    /**\n     * Convert any radix to a decimal number.\n     *\n     * @param input the string to be converted\n     * @param radix the radix (base) of the input string\n     * @return the decimal equivalent of the input string\n     * @throws NumberFormatException if the input string or radix is invalid\n     */\n    public static int convertToDecimal(String input, int radix) {\n        int result = 0;\n        int power = 1;\n\n        for (int i = input.length() - 1; i >= 0; i--) {\n            int digit = valOfChar(input.charAt(i));\n            if (digit >= radix) {\n                throw new NumberFormatException(\"For input string: \" + input);\n            }\n            result += digit * power;\n            power *= radix;\n        }\n        return result;\n    }\n\n    /**\n     * Convert a character to its integer value.\n     *\n     * @param character the character to be converted\n     * @return the integer value represented by the character\n     * @throws NumberFormatException if the character is not an uppercase letter or a digit\n     */\n    private static int valOfChar(char character) {\n        if (Character.isDigit(character)) {\n            return character - CHAR_OFFSET_FOR_DIGIT;\n        } else if (Character.isUpperCase(character)) {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java#L10-L46","documentation":"Thrown by AnyBaseToDecimal.convertToDecimal() when a digit's value is greater than or equal to the supplied radix (e.g., a digit '3' with radix 2, or 'F' with radix 10). This mimics java.lang.Integer.parseInt's behavior for invalid digits in the given base. The message echoes the offending input string.","triggerScenarios":"Calling convertToDecimal(\"12A\", 10) where 'A' has value 10 >= radix 10. Calling convertToDecimal(\"1002\", 2) where '2' is out of range for binary. Mixing base-N digits with the wrong radix argument.","commonSituations":"Inferring the wrong radix from the input format (treating hex as decimal). User-supplied number strings without validating the base first. Off-by-one in radix selection (using 2..36 range incorrectly).","solutions":["Confirm the radix matches the actual base of the input string before calling convertToDecimal().","Pre-validate each character against the radix: every digit must satisfy valOfChar(c) < radix.","Use Integer.parseInt(input, radix) if you want standard parsing, or wrap convertToDecimal in try/catch for NumberFormatException."],"exampleFix":"// before\nint dec = AnyBaseToDecimal.convertToDecimal(\"FF\", 10); // 'F' value 15 >= 10\n\n// after\nint dec = AnyBaseToDecimal.convertToDecimal(\"FF\", 16); // correct radix","handlingStrategy":"validation","validationCode":"for (int i = 0; i < input.length(); i++) {\n    char c = input.charAt(i);\n    int v = Character.isDigit(c) ? c - '0' : (Character.isUpperCase(c) ? c - 'A' + 10 : -1);\n    if (v < 0 || v >= radix) throw new IllegalArgumentException(\"digit out of range: \" + c);\n}\nint dec = AnyBaseToDecimal.convertToDecimal(input, radix);","typeGuard":"static boolean inputIsValidForRadix(String input, int radix) {\n    for (char c : input.toCharArray()) {\n        int v = Character.isDigit(c) ? c - '0' : (Character.isUpperCase(c) ? c - 'A' + 10 : -1);\n        if (v < 0 || v >= radix) return false;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    int dec = AnyBaseToDecimal.convertToDecimal(input, radix);\n} catch (NumberFormatException e) {\n    throw new DomainException(\"Invalid number for base \" + radix + \": \" + input, e);\n}","preventionTips":["Confirm the radix matches the actual base of the input before calling.","Remember this converter only accepts uppercase letters, not lowercase.","Use Integer.parseInt(input, radix) for case-insensitive standard parsing."],"tags":["conversions","base-conversion","radix","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}