TheAlgorithms/Java · error · IllegalArgumentException
Input '" + input + "' contains not only digits
Error message
Input '" + input + "' contains not only digits
What it means
Thrown by Verhoeff.checkInput (invoked by verhoeffCheck and addVerhoeffChecksum) when the input string does not match the regex \d+, i.e. contains any non-digit character or is empty. The Verhoeff algorithm operates purely on decimal digits via dihedral-group multiplication and permutation tables, so letters, spaces, dashes, or a blank string are invalid inputs.
Source
Thrown at src/main/java/com/thealgorithms/others/Verhoeff.java:164
System.out.println("\nCheck digit generation example:");
var input = "236";
generateAndPrint(input);
}
private static void checkAndPrint(String input) {
String validationResult = Verhoeff.verhoeffCheck(input) ? "valid" : "not valid";
System.out.println("Input '" + input + "' is " + validationResult);
}
private static void generateAndPrint(String input) {
String result = addVerhoeffChecksum(input);
System.out.println("Generate and add checksum to initial value '" + input + "'. Result: '" + result + "'");
}
private static void checkInput(String input) {
Objects.requireNonNull(input);
if (!input.matches("\\d+")) {
throw new IllegalArgumentException("Input '" + input + "' contains not only digits");
}
}
private static int[] toIntArray(String string) {
return string.chars().map(i -> Character.digit(i, 10)).toArray();
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Strip all non-digit characters from the input before calling: input.replaceAll("\\D+", "").
- Reject or re-prompt empty input upstream so the algorithm never receives "".
- Validate with input.matches("\\d+") (or Character.isDigit on each char) before invoking and surface a user-facing validation error instead of letting the exception propagate.
- Ensure the input has at least one digit; note that \d+ does not match the empty string.
Example fix
// before
boolean ok = Verhoeff.verhoeffCheck("123-456");
// after
String clean = "123-456".replaceAll("\\D+", "");
boolean ok = clean.isEmpty() ? false : Verhoeff.verhoeffCheck(clean); Defensive patterns
Strategy: validation
Validate before calling
String clean = input == null ? "" : input.replaceAll("\\D+", "");
if (clean.isEmpty()) {
// handle empty input in UI/parse layer
return;
}
boolean ok = Verhoeff.verhoeffCheck(clean); Type guard
static boolean isAllDigits(String s) {
return s != null && !s.isEmpty() && s.chars().allMatch(Character::isDigit);
} Try / catch
try {
boolean ok = Verhoeff.verhoeffCheck(raw);
} catch (IllegalArgumentException e) {
// surface a user-facing "invalid format" message
} Prevention
- Sanitise identifier strings (strip spaces, dashes) at the input boundary.
- Reject empty strings explicitly before calling — \d+ requires at least one digit.
- Use Character.isDigit in a loop if regex compilation cost matters.
When it happens
Trigger: Calling verhoeffCheck or addVerhoeffChecksum with a string containing hyphens ("123-45"), spaces, letters, a plus sign, a leading +, or an empty string "" (\d+ requires at least one digit).
Common situations: Passing a formatted identification number with separators (e.g. ISBN/Aadhaar-style "1234 5678 9012"), user input with whitespace not trimmed, copy-pasted values containing non-breaking spaces, or an empty form field.
Related errors
- Invalid unit '{}'. Supported units are: {}
- inputUnit must be different from outputUnit.
- NULL_INPUT
- UNKNOWN_WORD
- Cannot enqueue null data
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/800418187a10d3d1.
Report an issue: GitHub.