TheAlgorithms/Java · error · IllegalArgumentException
The number must be in the range [%d, %d]
Error message
The number must be in the range [%d, %d]
What it means
Thrown by RomanNumeralUtil.generate(int) when the input falls outside the supported additive Roman numeral range [1, 5999]. The lookup tables RN_M/RN_C/RN_X/RN_I only cover up to 5999 (MMMMM...), and zero/negative values have no Roman numeral representation, so the guard rejects them before an ArrayIndexOutOfBoundsException or garbage output could occur.
Source
Thrown at src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java:68
"XC",
};
// 1-9
private static final String[] RN_I = {
"",
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX",
};
public static String generate(int number) {
if (number < MIN_VALUE || number > MAX_VALUE) {
throw new IllegalArgumentException(String.format("The number must be in the range [%d, %d]", MIN_VALUE, MAX_VALUE));
}
return (RN_M[number / 1000] + RN_C[number % 1000 / 100] + RN_X[number % 100 / 10] + RN_I[number % 10]);
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp or reject the input before calling generate(), ensuring 1 <= number <= 5999.
- If you need values >= 6000, switch to a library that supports vinculum (overbar) notation instead of this util.
- Add a unit test asserting generate(1) and generate(5999) succeed while generate(0) and generate(6000) throw.
Example fix
// before
String r = RomanNumeralUtil.generate(userValue);
// after
if (userValue < 1 || userValue > 5999) {
throw new IllegalArgumentException("Value out of Roman range: " + userValue);
}
String r = RomanNumeralUtil.generate(userValue); Defensive patterns
Strategy: validation
Validate before calling
if (number < 1 || number > 5999) {
throw new IllegalArgumentException("Value out of Roman range [1,5999]: " + number);
}
String r = RomanNumeralUtil.generate(number); Try / catch
try {
String r = RomanNumeralUtil.generate(number);
} catch (IllegalArgumentException e) {
// surface a user-facing error; do not silently default
throw new DomainException("Unsupported value for Roman conversion", e);
} Prevention
- Treat the [1,5999] bound as part of the method's type contract and document it at every call site.
- For values >= 6000, select a different library supporting vinculum notation.
When it happens
Trigger: Call RomanNumeralUtil.generate(0), generate(-1), generate(6000), or generate with any int < 1 or > 5999. Also hit by passing Integer.MAX_VALUE or a parsed user value that overflowed a bound check upstream.
Common situations: Off-by-one loop counters starting at 0, unchecked external input parsed into an int, code that assumed Roman numerals are unbounded, or a refactor that changed the expected domain without updating callers.
Related errors
- Input array must have length of at least two
- numOfTerms nonnegative.
- n must be non-negative.
- Input must be non-negative
- sylvester() does not accept negative numbers or zero.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/45bce4099476840c.
Report an issue: GitHub.