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

  1. Decide on domain semantics: either reject negatives upstream as non-palindromes, or pass Math.abs(number) if magnitude-based checking is acceptable.
  2. Validate at the input boundary that the value is non-negative.
  3. 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

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


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/5a2fe47589e102ba. Report an issue: GitHub.