TheAlgorithms/Java · error · WordsToNumberException

MULTIPLE_NEGATIVES

MULTIPLE_NEGATIVES

Error message

Invalid Input. Multiple 'Negative's detected.

What it means

Thrown by handleNegative when the word 'negative' is encountered mid-phrase AND isNegative is already true (meaning a leading 'negative' was already consumed). The parser only allows a single leading 'negative' to negate the whole number; a second occurrence is an error.

Source

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

        if (!currentChunkIsZero && !isAdditionSafe(currentChunk, bigDecimalNumber)) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);
        }
        return currentChunk.add(bigDecimalNumber);
    }

    private static void handlePoint(Collection<BigDecimal> chunks, BigDecimal currentChunk, ArrayDeque<String> wordDeque) {
        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;
        if (!currentChunkIsZero) {
            chunks.add(currentChunk);
        }

        String decimalPart = convertDecimalPart(wordDeque);
        chunks.add(new BigDecimal(decimalPart));
    }

    private static void handleNegative(boolean isNegative) {
        if (isNegative) {
            throw new WordsToNumberException(WordsToNumberException.ErrorType.MULTIPLE_NEGATIVES, "");
        }
        throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_NEGATIVE, "");
    }

    private static BigDecimal convertWordQueueToBigDecimal(ArrayDeque<String> wordDeque) {
        BigDecimal currentChunk = BigDecimal.ZERO;
        List<BigDecimal> chunks = new ArrayList<>();

        boolean isNegative = "negative".equals(wordDeque.peek());
        if (isNegative) {
            wordDeque.poll();
        }

        boolean prevNumWasHundred = false;
        boolean prevNumWasPowerOfTen = false;

        while (!wordDeque.isEmpty()) {
            String word = wordDeque.poll();

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use 'negative' only once at the very start of the phrase.
  2. For expressions involving subtraction, evaluate them outside this converter.
  3. Validate that 'negative' appears at most once and only as the first token.

Example fix

// before
WordsToNumber.convert('negative negative five');
// after
WordsToNumber.convert('negative five'); // -5
Defensive patterns

Strategy: validation

Validate before calling

static String sanitizeNegatives(String s) {
    long count = Arrays.stream(s.trim().toLowerCase(Locale.ROOT).split("[ ,-]+")).filter("negative"::equals).count();
    if (count > 1) throw new IllegalArgumentException("Multiple 'negative' not allowed");
    return s;
}

Try / catch

try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {
    if (e.errorType == ErrorType.MULTIPLE_NEGATIVES) throw new ApiException(400, "Use 'negative' at most once");
    throw e;
}

Prevention

When it happens

Trigger: convert('negative five negative'), convert('negative negative five'), convert('negative one hundred negative two').

Common situations: Users trying to express subtraction or multiple negative parts; malformed input; LLM output that repeats negation.

Related errors


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