TheAlgorithms/Java · error · IllegalArgumentException
Number of discs must be non-negative
Error message
Number of discs must be non-negative
What it means
Thrown by TowerOfHanoi.shift(n, ...) when the disc count n is negative. The recursion shift(n-1,...) would never terminate for n < 0 (deeper into negatives), so the guard rejects negative inputs. n == 0 is valid (no moves, empty result).
Source
Thrown at src/main/java/com/thealgorithms/puzzlesandgames/TowerOfHanoi.java:61
* @param result A list to store the steps required to solve the puzzle.
* @throws IllegalArgumentException if {@code n} is negative.
*
* <p>
* This method is called recursively to move n-1 discs
* to the intermediate pole,
* then moves the nth disc to the end pole, and finally
* moves the n-1 discs from the
* intermediate pole to the end pole.
* </p>
*
* <p>
* Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.
* Space Complexity: O(n) - Linear space complexity due to the recursion stack.
* </p>
*/
public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) {
if (n < 0) {
throw new IllegalArgumentException("Number of discs must be non-negative");
}
if (n == 0) {
return;
}
// Move n-1 discs from startPole to intermediatePole
shift(n - 1, startPole, endPole, intermediatePole, result);
// Add the move of the nth disc from startPole to endPole
result.add(String.format("Move %d from %s to %s", n, startPole, endPole));
// Move the n-1 discs from intermediatePole to endPole
shift(n - 1, intermediatePole, startPole, endPole, result);
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp n to >= 0 before calling: Math.max(0, n).
- Validate n >= 0 at the caller and reject the request early with a domain error.
- Check for n <= 0 and return an empty result list without invoking shift.
Example fix
// before
TowerOfHanoi.shift(n, "A", "B", "C", moves); // n may be negative
// after
if (n < 0) throw new IllegalArgumentException("disc count must be >= 0");
TowerOfHanoi.shift(n, "A", "B", "C", moves); Defensive patterns
Strategy: validation
Validate before calling
if (n < 0) {
throw new IllegalArgumentException("disc count must be non-negative");
}
List<String> moves = new ArrayList<>();
TowerOfHanoi.shift(n, "A", "B", "C", moves); Type guard
static boolean validDiscCount(int n) {
return n >= 0;
} Try / catch
try {
TowerOfHanoi.shift(n, "A", "B", "C", moves);
} catch (IllegalArgumentException e) {
logger.warn("Negative disc count: {}", n);
} Prevention
- Validate disc count at the input boundary.
- Guard large n too — moves grow as 2^n and can exhaust memory/time.
- Pre-check n <= 0 to return an empty move list without invoking recursion.
When it happens
Trigger: Call shift(-1, ...) or shift(-5, ...). Any n < 0 trips it; the message says 'non-negative' and n=0 is permitted.
Common situations: Subtracting from an input that could go below zero (n - 1 where n was 0); parsing a negative disc count from config; pre-decrementing n before calling.
Related errors
- Target must be non-negative
- number is negative
- n must be a non-negative integer
- Input must be non-negative. Received:
- Input must be a positive integer. Provided:
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7ecaecd12d73790b.
Report an issue: GitHub.