Search code examples
javamethodsluhn

Luhn's algorithm


a) Starting with the check digit and moving left, compute the sum of all the decoded digits. b) Compute the remainder of the sum using integer division by 10. If the result is not zero, the credit card number is invalid. Otherwise, the card number is likely to be valid.

Here are two examples:

        Card number: 2315778     Card number 1234567
        decode(8, false) = 8     decode(7, false) = 7
        decode(7, true)  = 5     decode(6, true)  = 3
        decode(7, false) = 7     decode(5, false) = 5
        decode(5, true)  = 1     decode(4, true)  = 8
        decode(1, false) = 1     decode(3, false) = 3
        decode(3, true)  = 6     decode(2, true)  = 4
        decode(2, false) = 2     decode(1, false) = 1

                    Sum = 30                 Sum = 31
               30 mod 10 = 0            31 mod 10 = 1
This number may be valid    This number is invalid

Write a static method called checkDigits that is passed a seven-digit credit card number and that performs the steps described above. Reuse the decode method that you wrote in Lab 5.5.1. The method should return the word “valid” if the number passes the test and “invalid” otherwise.

import java.util.Scanner;

public class JavaApplication90 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int num = 2315778;
        System.out.println("Credit card number: " + num + " is " + checkDigits(num));
        num = 1234567;
        System.out.println("Credit card number: " + num + " is " + checkDigits(num));
        num = 7654321;
        System.out.println("Credit card number: " + num + " is " + checkDigits(num));
        num = 1111111;
        System.out.println("Credit card number: " + num + " is " + checkDigits(num));
    }

    public static boolean checkDigits(int num)
    {
        int sum = 0;
        String reverse = new StringBuffer(num).reverse().toString();
        for (int i = 0; i < reverse.length(); i++){
            int product = 0;
            if (i % 2 == 0)
            {
                product = num * 2;
            }
            if (product < 9)
                product = (product%10)-1;
            sum = sum+ product   ;           
        }
        return (sum % 10 == 0);
    }
}

Output:

I am getting true/valid answer for all numbers. I am not able to find my error. Help!


Solution

  • There are at least three issues.

    • You don't set product in the case where i is odd; so in that case, product will be 0 and the sum will be wrong.
    • You don't actually look for the ith digit of num. You're just using num wholesale each time you refer to it, whereas you should be using something like reverse.charAt(i) instead.
    • The StringBuffer that you're creating is actually empty - the constructor that you've used doesn't do what you think it does. This means that you're not iterating the for loop at all. You should probably check the Javadocs for a more suitable StringBuffer constructor to use.