TheAlgorithms/Java · error · NumberFormatException
invalid character:{}
Error message
invalid character:{} What it means
Thrown by the private AnyBaseToDecimal.valOfChar() helper when a character is neither a digit (0-9) nor an uppercase letter (A-Z). Lowercase letters, symbols, and whitespace are all rejected. This limits convertToDecimal to inputs using digits and uppercase A-Z only.
Source
Thrown at src/main/java/com/thealgorithms/conversions/AnyBaseToDecimal.java:49
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)) {
return character - CHAR_OFFSET_FOR_UPPERCASE;
} else {
throw new NumberFormatException("invalid character:" + character);
}
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Normalize input to uppercase before calling convertToDecimal(): input.toUpperCase(Locale.ROOT).
- Strip any non-alphanumeric characters (spaces, signs, '0x' prefixes) before conversion.
- If lowercase or extended alphabets are required, use Integer.parseInt(input, radix) which accepts lowercase.
Example fix
// before
int dec = AnyBaseToDecimal.convertToDecimal("ff", 16); // lowercase 'f' rejected
// after
int dec = AnyBaseToDecimal.convertToDecimal("ff".toUpperCase(Locale.ROOT), 16); Defensive patterns
Strategy: validation
Validate before calling
String normalized = input.toUpperCase(Locale.ROOT).replaceAll("[^0-9A-Z]", "");
for (char c : normalized.toCharArray()) {
if (!Character.isDigit(c) && !Character.isUpperCase(c)) {
throw new IllegalArgumentException("invalid char: " + c);
}
}
int dec = AnyBaseToDecimal.convertToDecimal(normalized, radix); Type guard
static boolean isUppercaseAlphaNumeric(String s) {
for (char c : s.toCharArray()) {
if (!Character.isDigit(c) && !Character.isUpperCase(c)) return false;
}
return true;
} Try / catch
try {
int dec = AnyBaseToDecimal.convertToDecimal(input, radix);
} catch (NumberFormatException e) {
// normalize and retry, or surface to user
throw new DomainException("Unparseable number: " + input, e);
} Prevention
- Always uppercase input before calling convertToDecimal.
- Strip prefixes ('0x'), whitespace, and signs before conversion.
- Switch to Integer.parseInt for lowercase-hex compatibility.
When it happens
Trigger: Passing lowercase hex digits ("ff"). Passing input with whitespace, punctuation, or a sign character ('+', '-'). Passing non-ASCII or Unicode digits.
Common situations: Hex input from a source that emits lowercase. User input containing leading/trailing spaces. Numbers prefixed with '0x' or '0X'. Locale differences producing non-ASCII digits.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- For input string: {}
- Bases must be between 2 and 10.
- Invalid Base64 character: {}
- Slope and intercept must be valid numbers.
- Input cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/784671f9e1070237.
Report an issue: GitHub.