TheAlgorithms/Java · error · IllegalArgumentException

{" + cardNumber + "} - is not a card number

Error message

{" + cardNumber + "} - is not a card number

What it means

Thrown by Luhn.CreditCard.fromString when the input, after removing spaces, is not exactly 16 characters of digits (DIGITS_COUNT = 16). The method requires a 16-digit numeric string before it even attempts the Luhn checksum; failing format yields this 'is not a card number' error.

Source

Thrown at src/main/java/com/thealgorithms/others/Luhn.java:114

     */
    /**
     * Object representation of credit card.
     */
    private record CreditCard(int[] digits) {
        private static final int DIGITS_COUNT = 16;

        /**
         * @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++) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Normalize the input: strip all non-digit characters, then verify length == 16 before calling.
  2. If the card may have a different length (e.g. AMEX), do not use this method; route to a length-agnostic validator.
  3. Validate format with a regex (\\d{16}) on the cleaned string before invoking fromString.
  4. Tell users to enter 16 digits with optional spaces only.

Example fix

// before
CreditCard cc = CreditCard.fromString(rawInput); // rawInput may contain dashes / wrong length

// after
String digits = rawInput.replaceAll("\\D", "");
if (digits.length() != 16) {
    throw new IllegalArgumentException("Expected 16 digits, got " + digits.length());
}
CreditCard cc = CreditCard.fromString(digits);
Defensive patterns

Strategy: validation

Validate before calling

String digits = cardNumber.replaceAll("\\D", "");
if (digits.length() != 16 || !digits.matches("\\d{16}")) {
    throw new IllegalArgumentException("Expected 16 digits; got: " + cardNumber);
}
CreditCard cc = CreditCard.fromString(digits);

Type guard

public static boolean looksLikeCardNumber(String s) {
    if (s == null) return false;
    String digits = s.replaceAll("\\D", "");
    return digits.length() == 16 && digits.matches("\\d+");
}

Try / catch

try {
    cc = CreditCard.fromString(raw);
} catch (IllegalArgumentException e) {
    // format problem: ask user to re-enter 16 digits
    errors.add("Card number must be 16 digits.");
}

Prevention

When it happens

Trigger: Calling CreditCard.fromString(s) where s trims to a length other than 16, or contains non-digit characters (letters, dashes, dots). Passing 15-digit or 17-digit numbers; passing numbers with '-' separators that are not stripped (only spaces are stripped).

Common situations: User typing a card number with dashes or dots; AMEX (15-digit) or other non-16-digit cards passed to this 16-digit-only API; copy-paste introducing hidden characters; truncation/padding mistakes.

Related errors


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