TheAlgorithms/Java · error · IllegalArgumentException
Incorrect length parameters: minLength must be <= maxLength
Error message
Incorrect length parameters: minLength must be <= maxLength and both must be > 0
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/others/PasswordGen.java:34
private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
private static final String DIGITS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*(){}?";
private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS;
private PasswordGen() {
}
/**
* Generates a random password with a length between minLength and maxLength.
*
* @param minLength The minimum length of the password.
* @param maxLength The maximum length of the password.
* @return A randomly generated password.
* @throws IllegalArgumentException if minLength is greater than maxLength or if either is non-positive.
*/
public static String generatePassword(int minLength, int maxLength) {
if (minLength > maxLength || minLength <= 0 || maxLength <= 0) {
throw new IllegalArgumentException("Incorrect length parameters: minLength must be <= maxLength and both must be > 0");
}
Random random = new Random();
List<Character> letters = new ArrayList<>();
for (char c : ALL_CHARACTERS.toCharArray()) {
letters.add(c);
}
// Inbuilt method to randomly shuffle a elements of a list
Collections.shuffle(letters);
StringBuilder password = new StringBuilder();
// Note that size of the password is also random
for (int i = random.nextInt(maxLength - minLength) + minLength; i > 0; --i) {
password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length())));
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before String pw = PasswordGen.generatePassword(12, 8); // min > max -> throws // after int min = Math.max(1, requestedMin); int max = Math.max(min, requestedMax); String pw = PasswordGen.generatePassword(min, max);
Defensive patterns
Strategy: validation
Validate before calling
int min = Math.max(1, minLength); int max = Math.max(min, maxLength); String pw = PasswordGen.generatePassword(min, max);
Type guard
public static boolean areValidLengths(int min, int max) {
return min > 0 && max > 0 && min <= max;
} Try / catch
try {
pw = PasswordGen.generatePassword(minLen, maxLen);
} catch (IllegalArgumentException e) {
pw = PasswordGen.generatePassword(12, 16); // sane defaults
} Prevention
- 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.
When it happens
Trigger: Calling generatePassword(minLength, maxLength) where minLength > maxLength, or minLength <= 0, or maxLength <= 0.
Common situations: 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.
Related errors
- Damping factor must be between 0 and 1
- Bit length must be at least {} for security.
- Given range of values is invalid!
- Input '{input}' contains not only digits
- Position must be between 0 and {array.length}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/9253a6d7298dc3f6.
Report an issue: GitHub.