TheAlgorithms/Java · error · WordsToNumberException

UNEXPECTED_WORD

UNEXPECTED_WORD

Error message

Invalid Input. Unexpected Word: {}

What it means

Thrown in handleHundred when the word 'hundred' is used incorrectly: either the accumulated current chunk is already >= 10 (meaning a tens word like 'twenty' was already placed before 'hundred', e.g. 'twenty hundred') or the previous word was a power of ten (e.g. 'thousand hundred'). The grammar only allows a single digit 1-9 (or nothing, implying 'one') before 'hundred'.

Source

Thrown at src/main/java/com/thealgorithms/conversions/WordsToNumber.java:150

        String nextWord = wordDeque.pollFirst();
        String afterNextWord = wordDeque.peekFirst();

        wordDeque.addFirst(nextWord);

        Integer number = NumberWord.getValue(nextWord);

        boolean isPrevWordValid = prevNumWasHundred || prevNumWasPowerOfTen;
        boolean isNextWordValid = number != null && (number >= 10 || afterNextWord == null || "point".equals(afterNextWord));

        if (!isPrevWordValid || !isNextWordValid) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_CONJUNCTION, "");
        }
    }

    private static BigDecimal handleHundred(BigDecimal currentChunk, String word, boolean prevNumWasPowerOfTen) {
        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;
        if (currentChunk.compareTo(BigDecimal.TEN) >= 0 || prevNumWasPowerOfTen) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);
        }
        if (currentChunkIsZero) {
            currentChunk = currentChunk.add(BigDecimal.ONE);
        }
        return currentChunk.multiply(BigDecimal.valueOf(100));
    }

    private static void handlePowerOfTen(List<BigDecimal> chunks, BigDecimal currentChunk, BigDecimal powerOfTen, String word, boolean prevNumWasPowerOfTen) {
        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;
        if (currentChunkIsZero || prevNumWasPowerOfTen) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);
        }
        BigDecimal nextChunk = currentChunk.multiply(powerOfTen);

        if (!(chunks.isEmpty() || isAdditionSafe(chunks.getLast(), nextChunk))) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);
        }
        chunks.add(nextChunk);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use the standard form: a single digit 1-9 before 'hundred', or omit it for implied 'one hundred'.
  2. For values like 1900, use 'one thousand nine hundred' instead of 'nineteen hundred'.
  3. Pre-validate that 'hundred' is only preceded by a units word (one-nine) or nothing.

Example fix

// before
WordsToNumber.convert('twenty hundred');
// after
WordsToNumber.convert('two thousand'); // 2000
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate: 'hundred' must be preceded by a 1-9 word or nothing, and not by a power of ten
static final Set<String> DIGITS = Set.of("one","two","three","four","five","six","seven","eight","nine");
static final Set<String> POWERS = Set.of("thousand","million","billion","trillion");
static boolean validHundred(String[] words) {
    for (int i = 0; i < words.length; i++) {
        if ("hundred".equals(words[i])) {
            if (i > 0 && (!DIGITS.contains(words[i-1]) || POWERS.contains(words[i-1]))) return false;
        }
    }
    return true;
}

Try / catch

try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {
    if (e.errorType == ErrorType.UNEXPECTED_WORD && input.contains("hundred")) throw new ApiException(400, "'hundred' must follow a single digit 1-9");
    throw e;
}

Prevention

When it happens

Trigger: convert('twenty hundred') (twenty >= 10), convert('eleven hundred'), convert('thousand hundred') (prevNumWasPowerOfTen true).

Common situations: Colloquial year-style phrasing ('nineteen hundred'); users expecting 'hundred' to act as a multiplier for any number; LLM output with non-standard phrasing.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/82131bd0acf49b14. Report an issue: GitHub.