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
- Sanitize each field to digits-only before formatting, or construct the input only from validated ints.
- Use a regex like ^\d{2}[-/]\d{2}[-/]\d{4}$ to pre-validate the whole string.
- 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
- Use a single regex to validate the whole date shape before handing off to the util.
- Construct input strings from validated ints rather than concatenating raw user text.
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
- Input date must be 10 characters long in the format MM-DD-YY
- Date separator must be '-' or '/'.
- Invalid date.
- Month must be between 1 and 12.
- Day must be between 1 and 31.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f8847eabace6e45e.
Report an issue: GitHub.