{"record":{"id":"84dbf663020557b5","repo":"TheAlgorithms/Java","slug":"invalid-negative","errorCode":"INVALID_NEGATIVE","errorMessage":"Invalid Input. Incorrect 'negative' placement","messagePattern":"Invalid Input\\. Incorrect 'negative' placement","errorType":"exception","errorClass":"WordsToNumberException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/WordsToNumber.java","lineNumber":198,"sourceCode":"        }\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();\n\n            switch (word) {","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/WordsToNumber.java#L180-L216","documentation":"Thrown by handleNegative when the word 'negative' appears anywhere other than the start. Because the parser only strips a leading 'negative' (setting isNegative=true and polling it), any subsequent 'negative' reaches handleNegative with isNegative=false, triggering INVALID_NEGATIVE. This means 'negative' is only legal as the very first token.","triggerScenarios":"convert('five negative'), convert('one hundred negative five'), convert('point five negative'). Any 'negative' that is not the first word.","commonSituations":"Users placing 'negative' after the number or mid-phrase; expecting per-component negation; LLM output with misplaced negation.","solutions":["Move 'negative' to the absolute beginning of the phrase.","Validate that if 'negative' is present, it is the first token and appears only once.","Reject inputs where 'negative' appears after position 0."],"exampleFix":"// before\nWordsToNumber.convert('five negative');\n// after\nWordsToNumber.convert('negative five'); // -5","handlingStrategy":"validation","validationCode":"static String sanitizeNegative(String s) {\n    String[] words = s.trim().toLowerCase(Locale.ROOT).split(\"[ ,-]+\");\n    for (int i = 1; i < words.length; i++) {\n        if (\"negative\".equals(words[i])) throw new IllegalArgumentException(\"'negative' only allowed at start\");\n    }\n    return s;\n}","typeGuard":null,"tryCatchPattern":"try { WordsToNumber.convert(input); } catch (WordsToNumberException e) {\n    if (e.errorType == ErrorType.INVALID_NEGATIVE) throw new ApiException(400, \"'negative' must be the first word\");\n    throw e;\n}","preventionTips":["Place 'negative' only as the first token.","Reject inputs where 'negative' appears after position 0.","Move any negation to the front of the phrase."],"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"}