Check Credit Card Validity using Luhn Algorithm

Viewed 55356

I tried to check the validation of credit card using Luhn algorithm, which works as the following steps:

  1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

    2 * 2 = 4

    2 * 2 = 4

    4 * 2 = 8

    1 * 2 = 2

    6 * 2 = 12 (1 + 2 = 3)

    5 * 2 = 10 (1 + 0 = 1)

    8 * 2 = 16 (1 + 6 = 7)

    4 * 2 = 8

  2. Now add all single-digit numbers from Step 1.

    4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

  3. Add all digits in the odd places from right to left in the card number.

    6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

  4. Sum the results from Step 2 and Step 3.

    37 + 38 = 75

  5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.

Simply, my program always displays valid for everything that I input. Even if it's a valid number and the result of sumOfOddPlace and sumOfDoubleEvenPlace methods are equal to zero.
Any help is appreciated.

import java.util.Scanner;
public class CreditCardValidation {
      public static void main(String[] args) {
     Scanner in = new Scanner(System.in);
        int count = 0;
        long array[] = new long [16];
       do
       {
        count = 0;
       array = new long [16];
        System.out.print("Enter your Credit Card Number : ");
        long number = in.nextLong();
        for (int i = 0; number != 0; i++) {
        array[i] = number % 10;
        number = number / 10;
        count++;
        }
       }
        while(count < 13); 
        if ((array[count - 1] == 4) || (array[count - 1] == 5) || (array[count - 1] == 3 && array[count - 2] == 7)){
            if (isValid(array) == true) {
                System.out.println("\n The Credit Card Number is Valid. ");
        } else {
            System.out.println("\n The Credit Card Number is Invalid. ");
        }
        } else{
          System.out.println("\n The Credit Card Number is Invalid. ");
        }
    }

    public static boolean isValid(long[] array) {
        int total = sumOfDoubleEvenPlace(array) + sumOfOddPlace(array);        
        if ((total % 10 == 0)) {
         for (int i=0; i< array.length; i++){
            System.out.println(array[i]);}
            return true;
        } else {
          for (int i=0; i< array.length; i++){
            System.out.println(array[i]);}
            return false;
        }
    }

    public static int getDigit(int number) {
        if (number <= 9) {
            return number;
        } else {
            int firstDigit = number % 10;
            int secondDigit = (int) (number / 10);
            return firstDigit + secondDigit;
        }
    }

    public static int sumOfOddPlace(long[] array) {
        int result = 0;
        for (int i=0; i< array.length; i++)
        {
        while (array[i] > 0) {
            result += (int) (array[i] % 10);
            array[i] = array[i] / 100;
         }}
         System.out.println("\n The sum of odd place is " + result);
        return result;
    }

    public static int sumOfDoubleEvenPlace(long[] array) {
        int result = 0;
        long temp = 0;
        for (int i=0; i< array.length; i++){
        while (array[i] > 0) {
             temp = array[i] % 100;
             result += getDigit((int) (temp / 10) * 2);
            array[i] = array[i] / 100;
           }
        }
        System.out.println("\n The sum of double even place is " + result);
        return result;
    }
     }
13 Answers

I took a stab at this with Java 8:

public static boolean luhn(String cc) {
    final boolean[] dbl = {false};
    return cc
            .chars()
            .map(c -> Character.digit((char) c, 10))
            .map(i -> ((dbl[0] = !dbl[0])) ? (((i*2)>9) ? (i*2)-9 : i*2) : i)
            .sum() % 10 == 0;
}

Add the line

            .replaceAll("\\s+", "")

Before

            .chars()

If you want to handle whitespace.

Seems to produce identical results to

return LuhnCheckDigit.LUHN_CHECK_DIGIT.isValid(cc);

From Apache's commons-validator.

private static int luhnAlgorithm(String number){
    int n=0;
    for(int i = 0; i<number.length(); i++){
        int x = Integer.parseInt(""+number.charAt(i));
        n += (x*Math.pow(2, i%2))%10;
        if (x>=5 && i%2==1) n++;
    }
    return n%10;
}
public class LuhnAlgorithm {

/**
 * Returns true if given card number is valid
 *
 * @param cardNum Card number
 * @return true if card number is valid else false
 */
private static boolean checkLuhn(String cardNum) {
    int cardlength = cardNum.length();
    int evenSum = 0, oddSum = 0, sum;
    for (int i = cardlength - 1; i >= 0; i--) {
        System.out.println(cardNum.charAt(i));
        int digit = Character.getNumericValue(cardNum.charAt(i));
        if (i % 2 == 0) {
            int multiplyByTwo = digit * 2;
            if (multiplyByTwo > 9) {
                /* Add two digits to handle cases that make two digits after doubling */
                String mul = String.valueOf(multiplyByTwo);
                multiplyByTwo = Character.getNumericValue(mul.charAt(0)) + Character.getNumericValue(mul.charAt(1));
            }
            evenSum += multiplyByTwo;
        } else {
            oddSum += digit;
        }
    }
    sum = evenSum + oddSum;
    if (sum % 10 == 0) {
        System.out.println("valid card");
        return true;
    } else {
        System.out.println("invalid card");
        return false;
    }

}

public static void main(String[] args) {
    String cardNum = "8112189875";
    System.out.println(checkLuhn(cardNum));
}

}

Hope it may works.

Here is the implementation of Luhn algorithm.

public class LuhnAlgorithm {

/**
 * Returns true if given card number is valid
 *
 * @param cardNum Card number
 * @return true if card number is valid else false
 */
private static boolean checkLuhn(String cardNum) {
    int cardlength = cardNum.length();
    int evenSum = 0, oddSum = 0, sum;
    for (int i = cardlength - 1; i >= 0; i--) {
        System.out.println(cardNum.charAt(i));
        int digit = Character.getNumericValue(cardNum.charAt(i));
        if (i % 2 == 0) {
            int multiplyByTwo = digit * 2;
            if (multiplyByTwo > 9) {
                /* Add two digits to handle cases that make two digits after doubling */
                String mul = String.valueOf(multiplyByTwo);
                multiplyByTwo = Character.getNumericValue(mul.charAt(0)) + Character.getNumericValue(mul.charAt(1));
            }
            evenSum += multiplyByTwo;
        } else {
            oddSum += digit;
        }
    }
    sum = evenSum + oddSum;
    if (sum % 10 == 0) {
        System.out.println("valid card");
        return true;
    } else {
        System.out.println("invalid card");
        return false;
    }

}

public static void main(String[] args) {
    String cardNum = "4071690065031703";
    System.out.println(checkLuhn(cardNum));
}

}

const options = {
  method: 'GET',
  headers: {Accept: 'application/json', 'X-Api-Key': '[APIkey]'}
};

fetch('https://api.epaytools.com/Tools/luhn?number=[CardNumber]&metaData=true', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Related