{"record":{"id":"82131bd0acf49b14","repo":"TheAlgorithms/Java","slug":"unexpected-word","errorCode":"UNEXPECTED_WORD","errorMessage":"Invalid Input. Unexpected Word: {}","messagePattern":"Invalid Input\\. Unexpected Word: (.+?)","errorType":"validation","errorClass":"WordsToNumberException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/WordsToNumber.java","lineNumber":150,"sourceCode":"        String nextWord = wordDeque.pollFirst();\n        String afterNextWord = wordDeque.peekFirst();\n\n        wordDeque.addFirst(nextWord);\n\n        Integer number = NumberWord.getValue(nextWord);\n\n        boolean isPrevWordValid = prevNumWasHundred || prevNumWasPowerOfTen;\n        boolean isNextWordValid = number != null && (number >= 10 || afterNextWord == null || \"point\".equals(afterNextWord));\n\n        if (!isPrevWordValid || !isNextWordValid) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_CONJUNCTION, \"\");\n        }\n    }\n\n    private static BigDecimal handleHundred(BigDecimal currentChunk, String word, boolean prevNumWasPowerOfTen) {\n        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;\n        if (currentChunk.compareTo(BigDecimal.TEN) >= 0 || prevNumWasPowerOfTen) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);\n        }\n        if (currentChunkIsZero) {\n            currentChunk = currentChunk.add(BigDecimal.ONE);\n        }\n        return currentChunk.multiply(BigDecimal.valueOf(100));\n    }\n\n    private static void handlePowerOfTen(List<BigDecimal> chunks, BigDecimal currentChunk, BigDecimal powerOfTen, String word, boolean prevNumWasPowerOfTen) {\n        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;\n        if (currentChunkIsZero || prevNumWasPowerOfTen) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);\n        }\n        BigDecimal nextChunk = currentChunk.multiply(powerOfTen);\n\n        if (!(chunks.isEmpty() || isAdditionSafe(chunks.getLast(), nextChunk))) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);\n        }\n        chunks.add(nextChunk);","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/WordsToNumber.java#L132-L168","documentation":"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'.","triggerScenarios":"convert('twenty hundred') (twenty >= 10), convert('eleven hundred'), convert('thousand hundred') (prevNumWasPowerOfTen true).","commonSituations":"Colloquial year-style phrasing ('nineteen hundred'); users expecting 'hundred' to act as a multiplier for any number; LLM output with non-standard phrasing.","solutions":["Use the standard form: a single digit 1-9 before 'hundred', or omit it for implied 'one hundred'.","For values like 1900, use 'one thousand nine hundred' instead of 'nineteen hundred'.","Pre-validate that 'hundred' is only preceded by a units word (one-nine) or nothing."],"exampleFix":"// before\nWordsToNumber.convert('twenty hundred');\n// after\nWordsToNumber.convert('two thousand'); // 2000","handlingStrategy":"validation","validationCode":"// Pre-validate: 'hundred' must be preceded by a 1-9 word or nothing, and not by a power of ten\nstatic final Set<String> DIGITS = Set.of(\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\");\nstatic final Set<String> POWERS = Set.of(\"thousand\",\"million\",\"billion\",\"trillion\");\nstatic boolean validHundred(String[] words) {\n    for (int i = 0; i < words.length; i++) {\n        if (\"hundred\".equals(words[i])) {\n            if (i > 0 && (!DIGITS.contains(words[i-1]) || POWERS.contains(words[i-1]))) return false;\n        }\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {\n    if (e.errorType == ErrorType.UNEXPECTED_WORD && input.contains(\"hundred\")) throw new ApiException(400, \"'hundred' must follow a single digit 1-9\");\n    throw e;\n}","preventionTips":["Place only a single digit (one-nine) before 'hundred'.","Never place a power-of-ten word immediately before 'hundred'.","For year-style numbers like 1900 use 'one thousand nine hundred' not 'nineteen hundred'."],"tags":["java","words-to-number","grammar","unexpected-word"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}