TheAlgorithms/Java · error · NumberFormatException
For input string: {}
Error message
For input string: {} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java:28
private AnyBaseToDecimal() {
}
/**
* Convert any radix to a decimal number.
*
* @param input the string to be converted
* @param radix the radix (base) of the input string
* @return the decimal equivalent of the input string
* @throws NumberFormatException if the input string or radix is invalid
*/
public static int convertToDecimal(String input, int radix) {
int result = 0;
int power = 1;
for (int i = input.length() - 1; i >= 0; i--) {
int digit = valOfChar(input.charAt(i));
if (digit >= radix) {
throw new NumberFormatException("For input string: " + input);
}
result += digit * power;
power *= radix;
}
return result;
}
/**
* Convert a character to its integer value.
*
* @param character the character to be converted
* @return the integer value represented by the character
* @throws NumberFormatException if the character is not an uppercase letter or a digit
*/
private static int valOfChar(char character) {
if (Character.isDigit(character)) {
return character - CHAR_OFFSET_FOR_DIGIT;
} else if (Character.isUpperCase(character)) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
int dec = AnyBaseToDecimal.convertToDecimal("FF", 10); // 'F' value 15 >= 10
// after
int dec = AnyBaseToDecimal.convertToDecimal("FF", 16); // correct radix Defensive patterns
Strategy: validation
Validate before calling
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
int v = Character.isDigit(c) ? c - '0' : (Character.isUpperCase(c) ? c - 'A' + 10 : -1);
if (v < 0 || v >= radix) throw new IllegalArgumentException("digit out of range: " + c);
}
int dec = AnyBaseToDecimal.convertToDecimal(input, radix); Type guard
static boolean inputIsValidForRadix(String input, int radix) {
for (char c : input.toCharArray()) {
int v = Character.isDigit(c) ? c - '0' : (Character.isUpperCase(c) ? c - 'A' + 10 : -1);
if (v < 0 || v >= radix) return false;
}
return true;
} Try / catch
try {
int dec = AnyBaseToDecimal.convertToDecimal(input, radix);
} catch (NumberFormatException e) {
throw new DomainException("Invalid number for base " + radix + ": " + input, e);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Slope and intercept must be valid numbers.
- invalid character:{}
- Bases must be between 2 and 10.
- Input cannot be null
- Invalid Base64 input length; must be multiple of 4
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/e78925aa385cf61c.
Report an issue: GitHub.