TheAlgorithms/Java · error · IllegalArgumentException
Input parameter must not be negative!
Error message
Input parameter must not be negative!
What it means
Thrown by PalindromeNumber.isPalindrome(int number) when number is negative. Negative integers cannot be palindromes by this implementation's definition (the minus sign is not a digit), and the digit-reversal loop assumes a non-negative input. The guard rejects negatives before reversing.
Source
Thrown at src/main/java/com/thealgorithms/maths/PalindromeNumber.java:26
* @see com.thealgorithms.stacks.PalindromeWithStack
* @see com.thealgorithms.bitmanipulation.BinaryPalindromeCheck
* @see com.thealgorithms.maths.LowestBasePalindrome
* @see com.thealgorithms.datastructures.lists.PalindromeSinglyLinkedList
* @see com.thealgorithms.maths.PalindromePrime
*/
public final class PalindromeNumber {
private PalindromeNumber() {
}
/**
* Check if {@code n} is palindrome number or not
*
* @param number the number
* @return {@code true} if {@code n} is palindrome number, otherwise
* {@code false}
*/
public static boolean isPalindrome(int number) {
if (number < 0) {
throw new IllegalArgumentException("Input parameter must not be negative!");
}
int numberCopy = number;
int reverseNumber = 0;
while (numberCopy != 0) {
int remainder = numberCopy % 10;
reverseNumber = reverseNumber * 10 + remainder;
numberCopy /= 10;
}
return number == reverseNumber;
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Decide on domain semantics: either reject negatives upstream as non-palindromes, or pass Math.abs(number) if magnitude-based checking is acceptable.
- Validate at the input boundary that the value is non-negative.
- Fix upstream logic so only non-negative values reach the call.
Example fix
// before boolean p = PalindromeNumber.isPalindrome(value); // after boolean p = value < 0 ? false : PalindromeNumber.isPalindrome(value);
Defensive patterns
Strategy: validation
Validate before calling
boolean p = number < 0 ? false : PalindromeNumber.isPalindrome(number);
Prevention
- Decide on domain semantics for negatives (typically not palindromes) and short-circuit.
- Validate parsed integers for sign before the call.
- Document whether abs()-based checking is acceptable for your use case.
When it happens
Trigger: Calling isPalindrome(-121) or any isPalindrome(number) where number < 0.
Common situations: Parsed integer from user input or a payload without sign validation; result of a computation (e.g., difference) fed in directly; assumption that the method handles negatives when it does not.
Related errors
- multiplicativePersistence() does not accept negative values
- additivePersistence() does not accept negative values
- number must be nonnegative.
- Input x-coordinates must be unique.
- Array should contain an even number of elements
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/5a2fe47589e102ba.
Report an issue: GitHub.