TheAlgorithms/Java · error · WordsToNumberException
UNKNOWN_WORD
UNKNOWN_WORD
Error message
Invalid Input. Unknown Word: {word} What it means
Thrown at the end of the main parsing loop when a word does not match any known category: not 'and', not 'hundred', not a power of ten, not a NumberWord, not 'point', not 'negative'. The default branch of the second switch falls through to this throw. The offending word is interpolated into the message.
Source
Thrown at src/main/java/com/thealgorithms/conversions/WordsToNumber.java:261
currentChunk = handleNumber(chunks, currentChunk, word, number);
continue;
}
switch (word) {
case "point" -> {
handlePoint(chunks, currentChunk, wordDeque);
currentChunk = BigDecimal.ZERO;
continue;
}
case "negative" -> {
handleNegative(isNegative);
}
default -> {
}
}
throw new WordsToNumberException(WordsToNumberException.ErrorType.UNKNOWN_WORD, word);
}
if (currentChunk.compareTo(BigDecimal.ZERO) != 0) {
chunks.add(currentChunk);
}
BigDecimal completeNumber = combineChunks(chunks);
return isNegative ? completeNumber.multiply(BigDecimal.valueOf(-1))
:
completeNumber;
}
private static boolean isAdditionSafe(BigDecimal currentChunk, BigDecimal number) {
int chunkDigitCount = currentChunk.toString().length();
int numberDigitCount = number.toString().length();
return chunkDigitCount > numberDigitCount;
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure all words are valid English number words from the supported vocabulary (zero-nineteen, twenty-ninety by tens, hundred, thousand, million, billion, trillion, point, negative, and).
- Spell-check and delimiter-check the input.
- If supporting other scales, pre-map them to the canonical vocabulary.
Example fix
// before
WordsToNumber.convert('one hunderd');
// after
WordsToNumber.convert('one hundred'); // 100 Defensive patterns
Strategy: validation
Validate before calling
static final Set<String> VOCAB = Set.of(
"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety","hundred","thousand","million","billion","trillion","point","negative","and");
static boolean allWordsKnown(String s) {
for (String w : s.trim().toLowerCase(Locale.ROOT).split("[ ,-]+")) {
if (!VOCAB.contains(w)) return false;
}
return true;
} Try / catch
try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {
if (e.errorType == ErrorType.UNKNOWN_WORD) throw new ApiException(400, "Unrecognized word in input: " + e.getMessage());
throw e;
} Prevention
- Validate every word against the supported vocabulary before calling convert.
- Spell-check and delimiter-check input.
- Map non-English or locale-specific scale words to canonical forms upstream.
When it happens
Trigger: convert('five baz'), convert('hello world'), convert('one hunderd') (typo), convert('twenty twohundred') (no space), convert('3.14') (numeric digits not words).
Common situations: Typos; non-English words; numeric digits instead of spelled-out words; concatenated words without delimiters; unsupported scale words like 'crore' or 'lakh'.
Related errors
- NULL_INPUT
- Invalid unit '{}'. Supported units are: {}
- inputUnit must be different from outputUnit.
- INVALID_CONJUNCTION
- UNEXPECTED_WORD
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/5a35c19d861ae614.
Report an issue: GitHub.