0

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!

Martin Thorsen Ranang
  • 2,394
  • 1
  • 28
  • 43
abcd248
  • 31
  • 1
  • 5
  • 4
    Can you properly indent your code please? – khelwood Nov 04 '14 at 23:38
  • 1
    this code is impossible to read, which is most likely why you have made a mistake. Please learn to use an IDEA and the auto format functionality. Never post unformatted code here. – Boris the Spider Nov 04 '14 at 23:42
  • You might want to use a debugger and look through the variables as they change. If you can't use a debugger, put `System.out.println` calls in `checkDigits` to see how your variables change as it progresses. If you do that you will see where your errors are. – ajb Nov 04 '14 at 23:46
  • @BoristheSpider I do hope you mean "an IDE", not "an idea". – Dawood ibn Kareem Nov 04 '14 at 23:48
  • 2
    @DavidWallace autocorrect strikes again. Maybe I meant the IDE "IDEA"... – Boris the Spider Nov 04 '14 at 23:51
  • 1
    Can you properly indent your code please? –first it is converting int to string – abcd248 Nov 04 '14 at 23:55
  • 1
    Can you properly indent your code please? first it is converting int to string; then if number is even, it will double it and if it is two digit, i added it. for example if number is 8 and it is at even position, it will double i.e 16 and 16 is two digit so i add 1+6 which is if(product<9) condition. then i added all the numbers and if the sum % 10 gives 0 remainder, it is valid oe else not – abcd248 Nov 04 '14 at 23:57
  • @EJP I disagree with closing this as a duplicate. This is certainly a homework problem, and pointing to a question that contains the answer is tantamount to doing her homework for her. – ajb Nov 05 '14 at 05:04

1 Answers1

1

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.
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110