TheAlgorithms/Java · error · IllegalArgumentException
Base must be greater than 1.
Error message
Base must be greater than 1.
What it means
Thrown by the private checkBase(int base) method, invoked by computeDigitsInBase(number, base) and isPalindromicInBase(number, base). A numeric base must be >= 2 because base-1 and base-0 representations are mathematically undefined for the digit-extraction algorithm used (number % base and number / base).
Source
Thrown at src/main/java/com/thealgorithms/maths/LowestBasePalindrome.java:46
* @see com.thealgorithms.bitmanipulation.BinaryPalindromeCheck
* @see com.thealgorithms.datastructures.lists.PalindromeSinglyLinkedList
* @see com.thealgorithms.maths.PalindromePrime
* @see com.thealgorithms.maths.PalindromeNumber
* @author TheAlgorithms Contributors
*/
public final class LowestBasePalindrome {
private LowestBasePalindrome() {
}
/**
* Validates the base, ensuring it is greater than 1.
*
* @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>View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the base parameter is >= 2 before calling computeDigitsInBase or isPalindromicInBase
- If accepting user input for base, validate and reject values below 2 at the input boundary
- Start base-iteration loops at 2
Example fix
// before
List<Integer> digits = LowestBasePalindrome.computeDigitsInBase(num, base);
// after
if (base < 2) throw new IllegalArgumentException("Base must be >= 2");
List<Integer> digits = LowestBasePalindrome.computeDigitsInBase(num, base); Defensive patterns
Strategy: validation
Validate before calling
if (base < 2) {
throw new IllegalArgumentException("Base must be >= 2: " + base);
}
List<Integer> digits = LowestBasePalindrome.computeDigitsInBase(number, base); Type guard
static boolean isValidBase(int base) {
return base >= 2;
} Prevention
- Always start base-iteration loops at 2, never 0 or 1
- Validate user-provided base values at the input boundary
- Remember that base-1 (unary) is not supported by this library
When it happens
Trigger: Calling LowestBasePalindrome.computeDigitsInBase(10, 1), computeDigitsInBase(10, 0), or isPalindromicInBase(15, 1). Any call with a base parameter <= 1.
Common situations: Passing a user-configured base value that defaults to 0 or 1. Off-by-one when iterating bases from 1 instead of 2. Confusing base-1 indexing with numeric radix.
Related errors
- Number must be non-negative. Given:
- Number must be positive
- Number must be non-negative.
- Number must be positive.
- Input must be non-negative. Received:
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/e314a1e9274387b7.
Report an issue: GitHub.