TheAlgorithms/Java · error · IllegalArgumentException
Input '{input}' contains not only digits
Error message
Input '{input}' contains not only digits What it means
Thrown by Damm.checkInput when the input string does not match the regex \d+ (one or more decimal digits). The Damm algorithm computes a check digit using a quasigroup operation table and requires a purely numeric input string; any non-digit character (letter, space, dash, sign, decimal point) is rejected. The message echoes the offending input verbatim.
Source
Thrown at src/main/java/com/thealgorithms/others/Damm.java:109
System.out.println("\nCheck digit generation example:");
var input = "572";
generateAndPrint(input);
}
private static void checkAndPrint(String input) {
String validationResult = Damm.dammCheck(input) ? "valid" : "not valid";
System.out.println("Input '" + input + "' is " + validationResult);
}
private static void generateAndPrint(String input) {
String result = addDammChecksum(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 non-digit characters and whitespace before calling: input.replaceAll("\\D+", "").
- Reject or normalize formatted identifiers (remove dashes/spaces) upstream.
- Validate with your own \d+ check first to give a clearer error context.
Example fix
// before
String id = "123-456-789";
Damm.dammCheck(id); // throws: contains dashes
// after
String id = "123-456-789".replaceAll("\\D+", "");
boolean ok = Damm.dammCheck(id); Defensive patterns
Strategy: validation
Validate before calling
String digits = input == null ? "" : input.replaceAll("\\D+", "");
if (digits.isEmpty()) {
throw new IllegalArgumentException("Input has no digits: " + input);
}
boolean ok = Damm.dammCheck(digits); Type guard
static boolean isAllDigits(String s) {
return s != null && s.matches("\\d+");
} Prevention
- Strip formatting characters (dashes, spaces) before checksumming.
- Validate identifiers are numeric at the point of ingestion.
When it happens
Trigger: Calling Damm.dammCheck/addDammChecksum with a string containing letters, whitespace, a leading sign (+/-), a decimal point, or formatting characters (dashes in a pseudo-account number). An empty string also fails \d+ (which requires at least one digit).
Common situations: Passing a formatted identifier (e.g., "123-456"), a numeric string with a leading '+', a value with surrounding whitespace, or a value that is actually alphanumeric.
Related errors
- Credit card number {" + cardNumber + "} - have a typo
- Input must be a non-empty binary string.
- Input must contain only '0' and '1'. Found: {}
- Input must contain only '0' and '1'.
- Input data contains invalid characters. Only uppercase A-Z a
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f6fd692eae5f8617.
Report an issue: GitHub.