{"record":{"id":"b509b1dfbfb77829","repo":"TheAlgorithms/Java","slug":"invalid-conjunction","errorCode":"INVALID_CONJUNCTION","errorMessage":"Invalid Input. Incorrect 'and' placement","messagePattern":"Invalid Input\\. Incorrect 'and' placement","errorType":"validation","errorClass":"WordsToNumberException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/WordsToNumber.java","lineNumber":129,"sourceCode":"\n    private static ArrayDeque<String> preprocessWords(String numberInWords) {\n        String[] wordSplitArray = numberInWords.trim().split(\"[ ,-]\");\n        ArrayDeque<String> wordDeque = new ArrayDeque<>();\n        for (String word : wordSplitArray) {\n            if (word.isEmpty()) {\n                continue;\n            }\n            wordDeque.add(word.toLowerCase());\n        }\n        if (wordDeque.isEmpty()) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.NULL_INPUT, \"\");\n        }\n        return wordDeque;\n    }\n\n    private static void handleConjunction(boolean prevNumWasHundred, boolean prevNumWasPowerOfTen, ArrayDeque<String> wordDeque) {\n        if (wordDeque.isEmpty()) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_CONJUNCTION, \"\");\n        }\n\n        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) {","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/WordsToNumber.java#L111-L147","documentation":"Thrown inside handleConjunction when the word deque is empty at the point 'and' is being processed — meaning 'and' was the last token in the input with no number following it. This is a structural error: English number grammar requires a number after 'and' (e.g., 'one hundred and five').","triggerScenarios":"Input ending with 'and': convert('one hundred and'), convert('twenty and'). Also when trailing delimiters produce 'and' as the final token.","commonSituations":"Truncated user input; copy-paste that cuts off mid-phrase; voice-to-text that drops the trailing number; building phrases programmatically and forgetting the final operand.","solutions":["Ensure every 'and' is followed by a number word (ten through ninety-nine forms).","Trim trailing 'and' from input before conversion if it is spurious.","Validate the phrase structure with a regex or grammar check before calling convert."],"exampleFix":"// before\nWordsToNumber.convert('one hundred and');\n// after\nString s = input.trim();\nif (s.endsWith(' and')) {\n    throw new IllegalArgumentException('phrase cannot end with \"and\"');\n}\nWordsToNumber.convert(s);","handlingStrategy":"validation","validationCode":"static String sanitize(String s) {\n    String t = s.trim();\n    if (t.endsWith(\" and\") || t.equals(\"and\")) throw new IllegalArgumentException(\"phrase cannot end with 'and'\");\n    return t;\n}","typeGuard":"static boolean endsWithConjunction(String s) {\n    return s != null && s.trim().toLowerCase(Locale.ROOT).endsWith(\" and\");\n}","tryCatchPattern":"try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {\n    if (e.errorType == ErrorType.INVALID_CONJUNCTION) throw new ApiException(400, \"'and' must be followed by a number\");\n    throw e;\n}","preventionTips":["Ensure every 'and' has a following number word.","Trim trailing 'and' from user input if it is unintentional.","Validate phrase completeness before conversion."],"tags":["java","words-to-number","grammar","conjunction"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}