TheAlgorithms/Java · error · IllegalArgumentException
Credit card number {" + cardNumber + "} - have a typo
Error message
Credit card number {" + cardNumber + "} - have a typo What it means
Thrown by Luhn.CreditCard.fromString when the input is a valid 16-digit string but fails the Luhn checksum. This indicates a syntactically well-formed number that is not a real/valid card number per the Luhn algorithm (likely a typo).
Source
Thrown at src/main/java/com/thealgorithms/others/Luhn.java:120
/**
* @param cardNumber string representation of credit card number - 16
* digits. Can have spaces for digits separation
* @return credit card object
* @throws IllegalArgumentException if input string is not 16 digits or
* if Luhn check was failed
*/
public static CreditCard fromString(String cardNumber) {
Objects.requireNonNull(cardNumber);
String trimmedCardNumber = cardNumber.replaceAll(" ", "");
if (trimmedCardNumber.length() != DIGITS_COUNT || !trimmedCardNumber.matches("\\d+")) {
throw new IllegalArgumentException("{" + cardNumber + "} - is not a card number");
}
int[] cardNumbers = toIntArray(trimmedCardNumber);
boolean isValid = luhnCheck(cardNumbers);
if (!isValid) {
throw new IllegalArgumentException("Credit card number {" + cardNumber + "} - have a typo");
}
return new CreditCard(cardNumbers);
}
/**
* @return string representation separated by space every 4 digits.
* Example: "5265 9251 6151 1412"
*/
public String number() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < DIGITS_COUNT; i++) {
if (i % 4 == 0 && i != 0) {
result.append(" ");
}
result.append(digits[i]);
}
return result.toString();View on GitHub (pinned to fdfb9a395b)
Solutions
- Prompt the user to re-enter the number; a Luhn failure almost always means a typo.
- If generating test data, use a Luhn-valid test card number (e.g. known test PANs).
- Compute and append the correct check digit programmatically before calling fromString.
- Double-check for digit transposition in upstream capture (OCR, keypunch).
Example fix
// before
CreditCard cc = CreditCard.fromString(userInput); // 16 digits but fails checksum
// after
// surface a user-facing error and request re-entry
try {
CreditCard cc = CreditCard.fromString(userInput);
} catch (IllegalArgumentException e) {
// tell the user the number looks mistyped; do not store it
} Defensive patterns
Strategy: try-catch
Validate before calling
String digits = cardNumber.replaceAll("\\D", "");
if (digits.length() != 16) throw new IllegalArgumentException("not 16 digits");
// Luhn pre-check (mirrors the library) to fail fast with context
int sum = 0; boolean alt = false;
for (int i = digits.length() - 1; i >= 0; i--, alt = !alt) {
int d = digits.charAt(i) - '0';
if (alt) { d *= 2; if (d > 9) d -= 9; }
sum += d;
}
if (sum % 10 != 0) throw new IllegalArgumentException("fails Luhn checksum"); Type guard
public static boolean passesLuhn(String digits) {
int sum = 0; boolean alt = false;
for (int i = digits.length() - 1; i >= 0; i--, alt = !alt) {
int d = digits.charAt(i) - '0';
if (alt) { d *= 2; if (d > 9) d -= 9; }
sum += d;
}
return sum % 10 == 0;
} Try / catch
try {
cc = CreditCard.fromString(raw);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("have a typo")) {
errors.add("Card number looks mistyped; please re-enter.");
} else {
throw e;
}
} Prevention
- A Luhn failure almost always means a typo; prompt for re-entry.
- Use known Luhn-valid test PANs in fixtures.
- Compute the correct check digit when generating synthetic numbers.
When it happens
Trigger: Calling CreditCard.fromString(s) where s is 16 digits but the Luhn check (luhnCheck) returns false; a single mistyped digit, a transposed pair of digits, or a fabricated 16-digit number.
Common situations: Manual data entry typo; OCR scan with one wrong digit; test fixture with a made-up number that happens to be 16 digits; transposed adjacent digits from copy-paste.
Related errors
- {" + cardNumber + "} - is not a card number
- Input '{input}' contains not only digits
- Position must be between 0 and {array.length}
- Array is empty
- Position must be between 0 and + (array.length - 1)
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/630bb7e56df98edf.
Report an issue: GitHub.