{"record":{"id":"15c2b2230274b20f","repo":"TheAlgorithms/Java","slug":"array-contains-non-positive-integers","errorCode":null,"errorMessage":"Array contains non-positive integers.","messagePattern":"Array contains non-positive integers\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/RadixSort.java","lineNumber":42,"sourceCode":"        if (array.length == 0) {\n            return array;\n        }\n\n        checkForNegativeInput(array);\n        radixSort(array);\n        return array;\n    }\n\n    /**\n     * Checks if the array contains any negative integers.\n     *\n     * @param array the array to be checked\n     * @throws IllegalArgumentException if any negative integers are found\n     */\n    private static void checkForNegativeInput(int[] array) {\n        for (int number : array) {\n            if (number < 0) {\n                throw new IllegalArgumentException(\"Array contains non-positive integers.\");\n            }\n        }\n    }\n\n    private static void radixSort(int[] array) {\n        final int max = Arrays.stream(array).max().getAsInt();\n        for (int i = 0, exp = 1; i < NumberOfDigits.numberOfDigits(max); i++, exp *= BASE) {\n            countingSortByDigit(array, exp);\n        }\n    }\n\n    /**\n     * A utility method to perform counting sort of array[] according to the digit represented by exp.\n     *\n     * @param array the array to be sorted\n     * @param exp   the exponent representing the current digit position\n     */\n    private static void countingSortByDigit(int[] array, int exp) {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/RadixSort.java#L24-L60","documentation":"Thrown by RadixSort.checkForNegativeInput(int[]) when any element is negative. This implementation uses counting sort per digit starting from the least significant digit and assumes non-negative values; negative numbers would be misclassified because their digit extraction differs. Note: the message says 'non-positive' but the code only checks number < 0, so zero is accepted.","triggerScenarios":"Calling sort with an array containing a negative value such as {4, -2, 7}; sorting signed data from sensors or deltas.","commonSituations":"Signed measurement data; using this RadixSort on general integers; assuming radix sort handles negatives like comparison sorts.","solutions":["Validate all elements >= 0 before calling sort.","For signed data, partition negatives and positives, sort each, then combine; or use Arrays.sort.","If the range is bounded, offset values to make them non-negative before sorting."],"exampleFix":"// before\nRadixSort.sort(data);\n\n// after\nif (Arrays.stream(data).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported\");\nRadixSort.sort(data);","handlingStrategy":"validation","validationCode":"if (Arrays.stream(array).anyMatch(v -> v < 0)) throw new IllegalArgumentException(\"negatives not supported by this RadixSort\");","typeGuard":"public static boolean isAllNonNegative(int[] a) { return Arrays.stream(a).allMatch(v -> v >= 0); }","tryCatchPattern":null,"preventionTips":["Note the message says 'non-positive' but only negatives are rejected; zero is allowed.","Validate the non-negative domain before sorting.","For signed data, partition by sign, sort each part, then recombine; or use Arrays.sort."],"tags":["sorting","non-negative-domain","input-validation","misleading-message"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}