TheAlgorithms/Java · error · IllegalArgumentException

Number must be non-negative.

Error message

Number must be non-negative.

What it means

Thrown by the private checkNumber(int number) method, invoked by computeDigitsInBase, isPalindromicInBase, and lowestBasePalindrome. The digit-extraction algorithm uses number % base and number / base, which behave differently for negative numbers in Java (sign-preserving remainder), producing incorrect digit lists. The guard rejects negatives to prevent silent wrong results.

Source

Thrown at src/main/java/com/thealgorithms/maths/LowestBasePalindrome.java:58

     *
     * @param base the base to be checked
     * @throws IllegalArgumentException if the base is less than or equal to 1
     */
    private static void checkBase(int base) {
        if (base <= 1) {
            throw new IllegalArgumentException("Base must be greater than 1.");
        }
    }

    /**
     * Validates the number, ensuring it is non-negative.
     *
     * @param number the number to be checked
     * @throws IllegalArgumentException if the number is negative
     */
    private static void checkNumber(int number) {
        if (number < 0) {
            throw new IllegalArgumentException("Number must be non-negative.");
        }
    }

    /**
     * Computes the digits of a given number in a specified base.
     * <p>
     * The digits are returned in reverse order (least significant digit first).
     * For example, the number 13 in base 2 produces [1,0,1,1] representing 1101 in
     * binary.
     * </p>
     *
     * @param number the number to be converted (must be non-negative)
     * @param base   the base to be used for the conversion (must be greater than 1)
     * @return a list of digits representing the number in the given base, with the
     *         least significant digit at the beginning of the list
     * @throws IllegalArgumentException if the number is negative or the base is
     *                                  less than 2
     */

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that number >= 0 before calling any LowestBasePalindrome method
  2. Reject negative inputs at the ingestion boundary
  3. Use Math.abs() only if the absolute value is meaningful for your use case

Example fix

// before
int base = LowestBasePalindrome.lowestBasePalindrome(userNumber);

// after
if (userNumber < 0) throw new IllegalArgumentException("Number must be non-negative");
int base = LowestBasePalindrome.lowestBasePalindrome(userNumber);
Defensive patterns

Strategy: validation

Validate before calling

if (number < 0) {
    throw new IllegalArgumentException("Number must be non-negative: " + number);
}
int base = LowestBasePalindrome.lowestBasePalindrome(number);

Type guard

static boolean isValidPalindromeInput(int number) {
    return number >= 0;
}

Prevention

When it happens

Trigger: Calling LowestBasePalindrome.lowestBasePalindrome(-5), computeDigitsInBase(-10, 2), or isPalindromicInBase(-3, 2). Any public method in the class called with a negative number.

Common situations: Passing a signed difference or unvalidated user input. Processing data where negative sentinel values represent missing entries.

Related errors


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