{"record":{"id":"9253a6d7298dc3f6","repo":"TheAlgorithms/Java","slug":"incorrect-length-parameters-minlength-must-be","errorCode":null,"errorMessage":"Incorrect length parameters: minLength must be <= maxLength and both must be > 0","messagePattern":"Incorrect length parameters: minLength must be <= maxLength and both must be > 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/PasswordGen.java","lineNumber":34,"sourceCode":"    private static final String LOWERCASE_LETTERS = \"abcdefghijklmnopqrstuvwxyz\";\n    private static final String DIGITS = \"0123456789\";\n    private static final String SPECIAL_CHARACTERS = \"!@#$%^&*(){}?\";\n    private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS;\n\n    private PasswordGen() {\n    }\n\n    /**\n     * Generates a random password with a length between minLength and maxLength.\n     *\n     * @param minLength The minimum length of the password.\n     * @param maxLength The maximum length of the password.\n     * @return A randomly generated password.\n     * @throws IllegalArgumentException if minLength is greater than maxLength or if either is non-positive.\n     */\n    public static String generatePassword(int minLength, int maxLength) {\n        if (minLength > maxLength || minLength <= 0 || maxLength <= 0) {\n            throw new IllegalArgumentException(\"Incorrect length parameters: minLength must be <= maxLength and both must be > 0\");\n        }\n\n        Random random = new Random();\n\n        List<Character> letters = new ArrayList<>();\n        for (char c : ALL_CHARACTERS.toCharArray()) {\n            letters.add(c);\n        }\n\n        // Inbuilt method to randomly shuffle a elements of a list\n        Collections.shuffle(letters);\n        StringBuilder password = new StringBuilder();\n\n        // Note that size of the password is also random\n        for (int i = random.nextInt(maxLength - minLength) + minLength; i > 0; --i) {\n            password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length())));\n        }\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/PasswordGen.java#L16-L52","documentation":"Thrown by PasswordGen.generatePassword when the length constraints are invalid: minLength > maxLength, or either bound is <= 0. The generator picks a random length in [minLength, maxLength] from a fixed character set, so an inverted or non-positive range has no valid length to pick.","triggerScenarios":"Calling generatePassword(minLength, maxLength) where minLength > maxLength, or minLength <= 0, or maxLength <= 0.","commonSituations":"Config with min greater than max (e.g. min=12, max=8); a 'min length' default of 0; UI controls allowing 0; bounds computed from user input without ordering/sanity checks.","solutions":["Ensure minLength <= maxLength and both are >= 1 before calling.","If you want a fixed-length password, pass the same value for both bounds.","Order/clamp the bounds from user input: min = max(1, min), max = max(min, max).","Validate the pair at the configuration boundary."],"exampleFix":"// before\nString pw = PasswordGen.generatePassword(12, 8); // min > max -> throws\n\n// after\nint min = Math.max(1, requestedMin);\nint max = Math.max(min, requestedMax);\nString pw = PasswordGen.generatePassword(min, max);","handlingStrategy":"validation","validationCode":"int min = Math.max(1, minLength);\nint max = Math.max(min, maxLength);\nString pw = PasswordGen.generatePassword(min, max);","typeGuard":"public static boolean areValidLengths(int min, int max) {\n    return min > 0 && max > 0 && min <= max;\n}","tryCatchPattern":"try {\n    pw = PasswordGen.generatePassword(minLen, maxLen);\n} catch (IllegalArgumentException e) {\n    pw = PasswordGen.generatePassword(12, 16); // sane defaults\n}","preventionTips":["Order and clamp bounds: min = max(1, min), max = max(min, max).","Pass equal values for a fixed-length password.","Validate the pair where user config enters the system."],"tags":["validation","password","security","input-validation","range"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}