TheAlgorithms/Java · error · IllegalArgumentException
Start must be non-negative. Given start:
Error message
Start must be non-negative. Given start:
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/maths/KaprekarNumbers.java:48
public final class KaprekarNumbers {
private KaprekarNumbers() {
}
/**
* Finds all Kaprekar numbers within a given range (inclusive).
*
* @param start the starting number of the range (inclusive)
* @param end the ending number of the range (inclusive)
* @return a list of all Kaprekar numbers in the specified range
* @throws IllegalArgumentException if start is greater than end or if start is
* negative
*/
public static List<Long> kaprekarNumberInRange(long start, long end) {
if (start > end) {
throw new IllegalArgumentException("Start must be less than or equal to end. Given start: " + start + ", end: " + end);
}
if (start < 0) {
throw new IllegalArgumentException("Start must be non-negative. Given start: " + start);
}
ArrayList<Long> list = new ArrayList<>();
for (long i = start; i <= end; i++) {
if (isKaprekarNumber(i)) {
list.add(i);
}
}
return list;
}
/**
* Checks whether a given number is a Kaprekar number.
* <p>
* The algorithm works as follows:
* <ol>
* <li>Square the number</li>View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end); // after long s = Math.max(0, Math.min(start, end)); long e = Math.max(start, end); List<Long> result = KaprekarNumbers.kaprekarNumberInRange(s, e);
Defensive patterns
Strategy: validation
Validate before calling
if (start < 0) {
throw new IllegalArgumentException("start must be non-negative: " + start);
}
List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end); Type guard
static boolean isNonNegativeStart(long start, long end) {
return start >= 0 && start <= end;
} Try / catch
try {
List<Long> result = KaprekarNumbers.kaprekarNumberInRange(start, end);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("non-negative")) {
// start < 0; clamp to 0
}
} Prevention
- Ensure start >= 0 before calling.
- Clamp negative start values to 0: start = Math.max(0, start).
- Validate both range bounds at the input boundary.
When it happens
Trigger: 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.
Common situations: User-supplied ranges starting from negative numbers. Data boundaries computed from offsets that can go below zero. Configuration with unvalidated lower bounds.
Related errors
- Start must be less than or equal to end. Given start: {start
- Input must be non-negative!
- Input 'n' is too big to give accurate result.
- k must be between 1 and the size of the array
- Array must be non-empty.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b780f5696f8e8f29.
Report an issue: GitHub.