{"record":{"id":"f33e86fe6dc62caa","repo":"TheAlgorithms/Java","slug":"start-must-be-less-than-or-equal-to-end-given-sta","errorCode":null,"errorMessage":"Start must be less than or equal to end. Given start: {start}, end: {end}","messagePattern":"Start must be less than or equal to end\\. Given start: (.+?), end: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/KaprekarNumbers.java","lineNumber":45,"sourceCode":" *      - Wikipedia</a>\n * @author TheAlgorithms (https://github.com/TheAlgorithms)\n */\npublic 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>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java#L27-L63","documentation":"Thrown by KaprekarNumbers.kaprekarNumberInRange(long start, long end) when start is strictly greater than end. The method iterates from start to end (inclusive) collecting Kaprekar numbers, so an inverted range would produce an empty result and is treated as a usage error. This is the first of two validation checks; it runs before the non-negative check (error 419).","triggerScenarios":"Calling kaprekarNumberInRange(100, 10), kaprekarNumberInRange(50, 49), or any case where start > end. Note: equal values (start == end) are valid and do NOT trigger this error — only strictly greater does.","commonSituations":"Range parameters from user input where start/end may be swapped. Computed ranges from sorting or filtering logic that occasionally invert. Configuration with min/max fields filled in wrong order.","solutions":["Ensure start <= end before calling kaprekarNumberInRange().","Swap start and end if the caller cannot guarantee ordering: if (start > end) { long t = start; start = end; end = t; }.","Validate range bounds at the input/configuration boundary."],"exampleFix":"// before\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(from, to);\n\n// after\nlong lo = Math.min(from, to);\nlong hi = Math.max(from, to);\nif (lo < 0) {\n    throw new IllegalArgumentException(\"Range must be non-negative: \" + lo);\n}\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(lo, hi);","handlingStrategy":"validation","validationCode":"if (start > end) {\n    throw new IllegalArgumentException(\"start must be <= end: \" + start + \" > \" + end);\n}\nList<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);","typeGuard":"static boolean isValidRange(long start, long end) {\n    return start <= end;\n}","tryCatchPattern":"try {\n    List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"less than or equal\")) {\n        // start > end; swap or reject\n    }\n}","preventionTips":["Ensure start <= end before calling.","Swap start/end if ordering cannot be guaranteed: long lo = Math.min(start, end).","Validate range parameters from user input or configuration."],"tags":["math","kaprekar","range","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}