{"record":{"id":"338acb4841368893","repo":"TheAlgorithms/Java","slug":"multiple-negatives","errorCode":"MULTIPLE_NEGATIVES","errorMessage":"Invalid Input. Multiple 'Negative's detected.","messagePattern":"Invalid Input\\. Multiple 'Negative's detected\\.","errorType":"validation","errorClass":"WordsToNumberException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/WordsToNumber.java","lineNumber":196,"sourceCode":"        if (!currentChunkIsZero && !isAdditionSafe(currentChunk, bigDecimalNumber)) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.UNEXPECTED_WORD, word);\n        }\n        return currentChunk.add(bigDecimalNumber);\n    }\n\n    private static void handlePoint(Collection<BigDecimal> chunks, BigDecimal currentChunk, ArrayDeque<String> wordDeque) {\n        boolean currentChunkIsZero = currentChunk.compareTo(BigDecimal.ZERO) == 0;\n        if (!currentChunkIsZero) {\n            chunks.add(currentChunk);\n        }\n\n        String decimalPart = convertDecimalPart(wordDeque);\n        chunks.add(new BigDecimal(decimalPart));\n    }\n\n    private static void handleNegative(boolean isNegative) {\n        if (isNegative) {\n            throw new WordsToNumberException(WordsToNumberException.ErrorType.MULTIPLE_NEGATIVES, \"\");\n        }\n        throw new WordsToNumberException(WordsToNumberException.ErrorType.INVALID_NEGATIVE, \"\");\n    }\n\n    private static BigDecimal convertWordQueueToBigDecimal(ArrayDeque<String> wordDeque) {\n        BigDecimal currentChunk = BigDecimal.ZERO;\n        List<BigDecimal> chunks = new ArrayList<>();\n\n        boolean isNegative = \"negative\".equals(wordDeque.peek());\n        if (isNegative) {\n            wordDeque.poll();\n        }\n\n        boolean prevNumWasHundred = false;\n        boolean prevNumWasPowerOfTen = false;\n\n        while (!wordDeque.isEmpty()) {\n            String word = wordDeque.poll();","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/WordsToNumber.java#L178-L214","documentation":"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.","triggerScenarios":"convert('negative five negative'), convert('negative negative five'), convert('negative one hundred negative two').","commonSituations":"Users trying to express subtraction or multiple negative parts; malformed input; LLM output that repeats negation.","solutions":["Use 'negative' only once at the very start of the phrase.","For expressions involving subtraction, evaluate them outside this converter.","Validate that 'negative' appears at most once and only as the first token."],"exampleFix":"// before\nWordsToNumber.convert('negative negative five');\n// after\nWordsToNumber.convert('negative five'); // -5","handlingStrategy":"validation","validationCode":"static String sanitizeNegatives(String s) {\n    long count = Arrays.stream(s.trim().toLowerCase(Locale.ROOT).split(\"[ ,-]+\")).filter(\"negative\"::equals).count();\n    if (count > 1) throw new IllegalArgumentException(\"Multiple 'negative' not allowed\");\n    return s;\n}","typeGuard":null,"tryCatchPattern":"try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {\n    if (e.errorType == ErrorType.MULTIPLE_NEGATIVES) throw new ApiException(400, \"Use 'negative' at most once\");\n    throw e;\n}","preventionTips":["Use 'negative' at most once, only at the start.","Handle subtraction outside this converter.","Count occurrences of 'negative' in the input before calling convert."],"tags":["java","words-to-number","grammar","negative"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}