How do I implement the Luhn algorithm?

后端 未结 8 1622
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 09:50

I am trying to create a program to validate 10 to 12 digit long number sequences based on the luhn algorithm, but my program keeps on telling me that every number is invalid

8条回答
  •  别那么骄傲
    2020-12-21 10:22

    Here's some functions I wrote to both calculate the check digit for a given number and to verify a given number sequence and extract the number from it.

    To calculate the check digit for a given number:

    /**
     * Generates the check digit for a number using Luhn's algorithm described in detail at the following link:
     * https://en.wikipedia.org/wiki/Luhn_algorithm
     *
     * In short the digit is calculated like so:
     * 1. From the rightmost digit moving left, double the value of every second digit. If that value is greater than 9,
     *    subtract 9 from it.
     * 2. Sum all of the digits together
     * 3. Multiply the sum by 9 and the check digit will be that value modulo 10.
     *
     * @param number the number to get the Luhn's check digit for
     * @return the check digit for the given number
     */
    public static int calculateLuhnsCheckDigit(final long number) {
        int     sum       = 0;
        boolean alternate = false;
        String  digits    = Long.toString(number);
    
        for (int i = digits.length() - 1; i >= 0; --i) {
            int digit = Character.getNumericValue(digits.charAt(i)); // get the digit at the given index
            digit = (alternate = !alternate) ? (digit * 2) : digit;  // double every other digit
            digit = (digit > 9)              ? (digit - 9) : digit;  // subtract 9 if the value is greater than 9
            sum += digit;                                            // add the digit to the sum
        }
    
        return (sum * 9) % 10;
    }
    

    To verify a sequence of digits using Luhn's algorithm and extract the number:

    /**
     * Verifies that a given number string is valid according to Luhn's algorithm, which is described in detail here:
     * https://en.wikipedia.org/wiki/Luhn_algorithm
     *
     * In short, validity of the number is determined like so:
     * 1. From the rightmost digit (the check digit) moving left, double the value of every second digit. The check
     *    digit is not doubled; the first digit doubled is the one immediately to the left of the check digit. If that
     *    value is greater than 9, subtract 9 from it.
     * 2. Sum all of the digits together
     * 3. If the sum modulo 10 is equal to 0, then the number is valid according to Luhn's algorithm
     *
     * @param luhnsNumber the number string to verify and extract the number from
     * @return an empty Optional if the given string was not valid according to Luhn's algorithm
     *         an Optional containing the number verified by Luhn's algorithm if the given string passed the check
     */
    public static Optional extractLuhnsNumber(final String luhnsNumber) {
        int     sum       = 0;
        boolean alternate = true;
        Long    number    = Long.parseLong(luhnsNumber.substring(0, luhnsNumber.length() - 1));
    
        for (int i = luhnsNumber.length() - 1; i >= 0; --i) {
            int digit = Character.getNumericValue(luhnsNumber.charAt(i)); // get the digit at the given index
            digit = (alternate = !alternate) ? (digit * 2) : digit;       // double every other digit
            digit = (digit > 9)              ? (digit - 9) : digit;       // subtract 9 if the value is greater than 9
            sum += digit;                                                 // add the digit to the sum
        }
    
        return (sum % 10 == 0) ? Optional.of(number) : Optional.empty();
    }
    

提交回复
热议问题