TheAlgorithms/Java · error · WordsToNumberException
INVALID_CONJUNCTION
INVALID_CONJUNCTION
Error message
Invalid Input. Incorrect 'and' placement
What it means
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').
Source
Thrown at src/main/java/com/thealgorithms/conversions/WordsToNumber.java:129
private static ArrayDeque<String> preprocessWords(String numberInWords) {
String[] wordSplitArray = numberInWords.trim().split("[ ,-]");
ArrayDeque<String> wordDeque = new ArrayDeque<>();
for (String word : wordSplitArray) {
if (word.isEmpty()) {
continue;
}
wordDeque.add(word.toLowerCase());
}
if (wordDeque.isEmpty()) {
throw new WordsToNumberException(WordsToNumberException.ErrorType.NULL_INPUT, "");
}
return wordDeque;
}
private static void handleConjunction(boolean prevNumWasHundred, boolean prevNumWasPowerOfTen, ArrayDeque<String> wordDeque) {
if (wordDeque.isEmpty()) {
throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_CONJUNCTION, "");
}
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) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
WordsToNumber.convert('one hundred and');
// after
String s = input.trim();
if (s.endsWith(' and')) {
throw new IllegalArgumentException('phrase cannot end with "and"');
}
WordsToNumber.convert(s); Defensive patterns
Strategy: validation
Validate before calling
static String sanitize(String s) {
String t = s.trim();
if (t.endsWith(" and") || t.equals("and")) throw new IllegalArgumentException("phrase cannot end with 'and'");
return t;
} Type guard
static boolean endsWithConjunction(String s) {
return s != null && s.trim().toLowerCase(Locale.ROOT).endsWith(" and");
} Try / catch
try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {
if (e.errorType == ErrorType.INVALID_CONJUNCTION) throw new ApiException(400, "'and' must be followed by a number");
throw e;
} Prevention
- Ensure every 'and' has a following number word.
- Trim trailing 'and' from user input if it is unintentional.
- Validate phrase completeness before conversion.
When it happens
Trigger: Input ending with 'and': convert('one hundred and'), convert('twenty and'). Also when trailing delimiters produce 'and' as the final token.
Common situations: 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.
Related errors
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b509b1dfbfb77829.
Report an issue: GitHub.