{"record":{"id":"b780f5696f8e8f29","repo":"TheAlgorithms/Java","slug":"start-must-be-non-negative-given-start","errorCode":null,"errorMessage":"Start must be non-negative. Given start: ","messagePattern":"Start must be non-negative\\. Given start: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/KaprekarNumbers.java","lineNumber":48,"sourceCode":"public final class KaprekarNumbers {\n    private KaprekarNumbers() {\n    }\n\n    /**\n     * Finds all Kaprekar numbers within a given range (inclusive).\n     *\n     * @param start the starting number of the range (inclusive)\n     * @param end   the ending number of the range (inclusive)\n     * @return a list of all Kaprekar numbers in the specified range\n     * @throws IllegalArgumentException if start is greater than end or if start is\n     *                                  negative\n     */\n    public static List<Long> kaprekarNumberInRange(long start, long end) {\n        if (start > end) {\n            throw new IllegalArgumentException(\"Start must be less than or equal to end. Given start: \" + start + \", end: \" + end);\n        }\n        if (start < 0) {\n            throw new IllegalArgumentException(\"Start must be non-negative. Given start: \" + start);\n        }\n\n        ArrayList<Long> list = new ArrayList<>();\n        for (long i = start; i <= end; i++) {\n            if (isKaprekarNumber(i)) {\n                list.add(i);\n            }\n        }\n\n        return list;\n    }\n\n    /**\n     * Checks whether a given number is a Kaprekar number.\n     * <p>\n     * The algorithm works as follows:\n     * <ol>\n     * <li>Square the number</li>","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java#L30-L66","documentation":"Thrown by KaprekarNumbers.kaprekarNumberInRange(long start, long end) when start is negative. Kaprekar numbers are defined for non-negative integers (0 and 1 are considered Kaprekar by isKaprekarNumber). This check runs after the start > end check (error 418), so a negative start that is also > end would hit the range error first. The error message appends the start value. Note: the Javadoc says 'start is negative' but only start is checked — end is implicitly allowed to be any value >= start.","triggerScenarios":"Calling kaprekarNumberInRange(-5, 10), kaprekarNumberInRange(-100, -1) (but -100 > -1 is false, so the range check passes and this fires), or any case where start < 0 and start <= end. The internal isKaprekarNumber also checks for negative num, so without this range-level guard, individual negative values would throw deeper.","commonSituations":"User-supplied ranges starting from negative numbers. Data boundaries computed from offsets that can go below zero. Configuration with unvalidated lower bounds.","solutions":["Ensure start >= 0 before calling kaprekarNumberInRange().","Clamp start to 0 if negative values may occur: start = Math.max(0, start).","Validate both range bounds at the input boundary before invoking."],"exampleFix":"// before\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);\n\n// after\nlong s = Math.max(0, Math.min(start, end));\nlong e = Math.max(start, end);\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(s, e);","handlingStrategy":"validation","validationCode":"if (start < 0) {\n    throw new IllegalArgumentException(\"start must be non-negative: \" + start);\n}\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);","typeGuard":"static boolean isNonNegativeStart(long start, long end) {\n    return start >= 0 && start <= end;\n}","tryCatchPattern":"try {\n    List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"non-negative\")) {\n        // start < 0; clamp to 0\n    }\n}","preventionTips":["Ensure start >= 0 before calling.","Clamp negative start values to 0: start = Math.max(0, start).","Validate both range bounds at the input boundary."],"tags":["math","kaprekar","range","argument-validation","non-negative"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}