{"record":{"id":"630bb7e56df98edf","repo":"TheAlgorithms/Java","slug":"credit-card-number-cardnumber-have-a-t","errorCode":null,"errorMessage":"Credit card number {\" + cardNumber + \"} - have a typo","messagePattern":"Credit card number (.+?) - have a typo","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/Luhn.java","lineNumber":120,"sourceCode":"\n        /**\n         * @param cardNumber string representation of credit card number - 16\n         * digits. Can have spaces for digits separation\n         * @return credit card object\n         * @throws IllegalArgumentException if input string is not 16 digits or\n         * if Luhn check was failed\n         */\n        public static CreditCard fromString(String cardNumber) {\n            Objects.requireNonNull(cardNumber);\n            String trimmedCardNumber = cardNumber.replaceAll(\" \", \"\");\n            if (trimmedCardNumber.length() != DIGITS_COUNT || !trimmedCardNumber.matches(\"\\\\d+\")) {\n                throw new IllegalArgumentException(\"{\" + cardNumber + \"} - is not a card number\");\n            }\n\n            int[] cardNumbers = toIntArray(trimmedCardNumber);\n            boolean isValid = luhnCheck(cardNumbers);\n            if (!isValid) {\n                throw new IllegalArgumentException(\"Credit card number {\" + cardNumber + \"} - have a typo\");\n            }\n\n            return new CreditCard(cardNumbers);\n        }\n\n        /**\n         * @return string representation separated by space every 4 digits.\n         * Example: \"5265 9251 6151 1412\"\n         */\n        public String number() {\n            StringBuilder result = new StringBuilder();\n            for (int i = 0; i < DIGITS_COUNT; i++) {\n                if (i % 4 == 0 && i != 0) {\n                    result.append(\" \");\n                }\n                result.append(digits[i]);\n            }\n            return result.toString();","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/Luhn.java#L102-L138","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\nCreditCard cc = CreditCard.fromString(userInput); // 16 digits but fails checksum\n\n// after\n// surface a user-facing error and request re-entry\ntry {\n    CreditCard cc = CreditCard.fromString(userInput);\n} catch (IllegalArgumentException e) {\n    // tell the user the number looks mistyped; do not store it\n}","handlingStrategy":"try-catch","validationCode":"String digits = cardNumber.replaceAll(\"\\\\D\", \"\");\nif (digits.length() != 16) throw new IllegalArgumentException(\"not 16 digits\");\n// Luhn pre-check (mirrors the library) to fail fast with context\nint sum = 0; boolean alt = false;\nfor (int i = digits.length() - 1; i >= 0; i--, alt = !alt) {\n    int d = digits.charAt(i) - '0';\n    if (alt) { d *= 2; if (d > 9) d -= 9; }\n    sum += d;\n}\nif (sum % 10 != 0) throw new IllegalArgumentException(\"fails Luhn checksum\");","typeGuard":"public static boolean passesLuhn(String digits) {\n    int sum = 0; boolean alt = false;\n    for (int i = digits.length() - 1; i >= 0; i--, alt = !alt) {\n        int d = digits.charAt(i) - '0';\n        if (alt) { d *= 2; if (d > 9) d -= 9; }\n        sum += d;\n    }\n    return sum % 10 == 0;\n}","tryCatchPattern":"try {\n    cc = CreditCard.fromString(raw);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"have a typo\")) {\n        errors.add(\"Card number looks mistyped; please re-enter.\");\n    } else {\n        throw e;\n    }\n}","preventionTips":["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."],"tags":["validation","luhn","credit-card","checksum","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}