TheAlgorithms/Java · error · IllegalArgumentException

Invalid numeric part: {part}

Error message

Invalid numeric part: {part}

What it means

Thrown by ZellersCongruence.parsePart when Integer.parseInt(part) raises NumberFormatException, i.e., one of the fixed substrings (month/day/year) contains non-digit characters. The original NumberFormatException is chained as the cause, and the offending substring is echoed in the message.

Source

Thrown at src/main/java/com/thealgorithms/maths/ZellersCongruence.java:92

    /**
     * Parses a part of the date string and validates its range.
     *
     * @param part  the substring to parse
     * @param min   the minimum valid value
     * @param max   the maximum valid value
     * @param error the error message to throw if validation fails
     * @return the parsed integer value
     * @throws IllegalArgumentException if the part is not a valid number or is out of range
     */
    private static int parsePart(String part, int min, int max, String error) {
        try {
            int value = Integer.parseInt(part);
            if (value < min || value > max) {
                throw new IllegalArgumentException(error);
            }
            return value;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid numeric part: " + part, e);
        }
    }

    /**
     * Validates the separator character in the date string.
     *
     * @param sep the separator character
     * @throws IllegalArgumentException if the separator is not '-' or '/'
     */
    private static void validateSeparator(char sep) {
        if (sep != '-' && sep != '/') {
            throw new IllegalArgumentException("Date separator must be '-' or '/'.");
        }
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Sanitize each field to digits-only before formatting, or construct the input only from validated ints.
  2. Use a regex like ^\d{2}[-/]\d{2}[-/]\d{4}$ to pre-validate the whole string.
  3. Parse with DateTimeFormatter which yields clearer date-parse errors.

Example fix

// before
String day = ZellersCongruence.calculateDay(raw);

// after
if (!raw.matches("\\d{2}[-/]\\d{2}[-/]\\d{4}")) {
    throw new IllegalArgumentException("Bad date format: " + raw);
}
String day = ZellersCongruence.calculateDay(raw);
Defensive patterns

Strategy: validation

Validate before calling

if (!input.matches("\\d{2}[-/]\\d{2}[-/]\\d{4}")) {
    throw new IllegalArgumentException("Date fields must be numeric with '-' or '/' separators");
}
String d = ZellersCongruence.calculateDay(input);

Prevention

When it happens

Trigger: Call calculateDay("0a-01-2020"), calculateDay("01- 1-2020") (space), calculateDay("AB-CD-EFGH"), or any input where a numeric field contains a letter/space/symbol.

Common situations: Mixed locale separators leaking into the digit fields, OCR/copy-paste artifacts (e.g., non-breaking space), user-typed alphabetic month abbreviations ('Jan'), or a field that was never sanitized.

Related errors


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