TheAlgorithms/Java · error · IllegalArgumentException

Input value must be a positive integer. Input value: {number

Error message

Input value must be a positive integer. Input value: {number}

What it means

Thrown by GermainPrimeAndSafePrime.isGermainPrime(int number) when number is less than 1. A Germain prime is a prime p where 2p+1 is also prime, which requires p >= 2 (the smallest prime). The method rejects all non-positive inputs before delegating to PrimeCheck.isPrime. The exception message includes the offending value for diagnostics.

Source

Thrown at src/main/java/com/thealgorithms/maths/GermainPrimeAndSafePrime.java:41

 */
public final class GermainPrimeAndSafePrime {

    // Private constructor to prevent instantiation
    private GermainPrimeAndSafePrime() {
    }

    /**
     * Checks if a number is a Germain prime.
     *
     * <p>A Germain prime is a prime number p such that 2p + 1 is also prime.
     *
     * @param number the number to check; must be a positive integer
     * @return {@code true} if the number is a Germain prime, {@code false} otherwise
     * @throws IllegalArgumentException if the input number is less than 1
     */
    public static boolean isGermainPrime(int number) {
        if (number < 1) {
            throw new IllegalArgumentException("Input value must be a positive integer. Input value: " + number);
        }
        // A number is a Germain prime if it is prime and 2 * number + 1 is also prime
        return PrimeCheck.isPrime(number) && PrimeCheck.isPrime(2 * number + 1);
    }

    /**
     * Checks if a number is a Safe prime.
     *
     * <p>A Safe prime is a prime number p such that (p - 1) / 2 is also prime.
     *
     * @param number the number to check; must be a positive integer
     * @return {@code true} if the number is a Safe prime, {@code false} otherwise
     * @throws IllegalArgumentException if the input number is less than 1
     */
    public static boolean isSafePrime(int number) {
        if (number < 1) {
            throw new IllegalArgumentException("Input value must be a positive integer. Input value: " + number);
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure number >= 1 before calling isGermainPrime (practically >= 2 since 1 is not prime).
  2. Sanitize or clamp user input to positive integers at the entry point.
  3. Wrap calls in try-catch(IllegalArgumentException) if input cannot be guaranteed.

Example fix

// before
boolean result = GermainPrimeAndSafePrime.isGermainPrime(userInput);

// after
if (userInput < 2) {
    return false; // or throw a domain-specific exception
}
boolean result = GermainPrimeAndSafePrime.isGermainPrime(userInput);
Defensive patterns

Strategy: validation

Validate before calling

if (number < 1) {
    return false; // or throw a domain-specific exception
}
boolean result = GermainPrimeAndSafePrime.isGermainPrime(number);

Type guard

static boolean isPositive(int n) {
    return n >= 1;
}

Try / catch

try {
    boolean result = GermainPrimeAndSafePrime.isGermainPrime(number);
} catch (IllegalArgumentException e) {
    // number < 1; not a valid candidate
    result = false;
}

Prevention

When it happens

Trigger: Calling isGermainPrime(0), isGermainPrime(-1), or any negative integer. The method is a pure validation gate — any number >= 1 proceeds to the prime check, but mathematically only p >= 2 can be a Germain prime.

Common situations: Processing user-supplied integers without sanitization. Looping over ranges that start at or below zero. Receiving unvalidated input from external sources, configuration files, or parsed strings.

Related errors


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