-6

I am working on a Credit Card Number validation application.

I can enter in either a valid number or invalid but the output never takes data in and reads it as valid. I'm trying to figure out what is the right algorithm for validation of credit card.

Below is my desired outcome. I need to know at what point of my loop, or my usage of long vs. string am I messing up on that would cause the output to always read as invalid.

Sample 1:

Enter a credit card number: 4246345689049834

4246345689049834 is invalid

Sample 2:

Enter a credit card number: 4388576018410707

4388576018410707 is valid

However, in my code, I get the below output

Sample 1:

Enter a credit card number: 4246345689049834

4246345689049834 is invalid

Sample 2:

Enter a credit card number: 4388576018410707

4388576018410707 is invalid 

Code:

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter Credit Card Number for Validation: ");
        long number = input.nextLong();

        long total = sumOfEvenPlaces(number) + sumOfOddPlaces(number);

        if (isValid(total)) {
            System.out.println(number + " is valid");
        } else {
            System.out.println(number + " is invalid");
        }
    }

    public static boolean isValid(long total) {
        if (total % 10 == 0) {
            return true;
        }
        return false;
    }

    public static int sumOfEvenPlaces(long number) {
        int sum = 0;
        int remainder;
        number %= 10;
        while (number % 10 != 0 || number / 10 != 0) {
            remainder = (int) (number % 10);

            sum = sum + getDigit(remainder * 2);
            number /= 100;
        }
        return sum;
    }

    public static int getDigit(int number) {
        if (number > 9) {
            return (number % 10 + number / 10);
        }
        return number;
    }

    public static int sumOfOddPlaces(long number) {
        int sum = 0;
        int remainder;

        while (number % 10 != 0 || number / 10 != 0) {
            remainder = (int) (number % 10);
            sum = sum + getDigit(remainder);
            number /= 100;
        }
        return sum;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rob Nelson
  • 11
  • 8
  • 2
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). **Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow.** If you are asking someone else to run your code, debug it in a step-debugger and fix it for you, you should know that is not how the site works. This is explained in the links I posted in my other comments. *debug my code dump for me is not a question either* –  Aug 04 '18 at 21:28

1 Answers1

1

You have mistake in both sumOfEvenPlaces() and sumOfOddPlaces() methods.

In sumOfEvenPlaces(long number) you have to change

 number %= 10; 

to

 number /= 10;

You can only skip a digit using number / 10 not by number % 10

And in both functions change

 while (number % 10 != 0 || number / 10 != 0)

to

 while (number > 0)

by using while (number > 0) the loop will work until all digits in number are processed.

Try these modified methods , This will work :-

public static int sumOfEvenPlaces(long number) {
    int sum = 0;
    int remainder;
    number /= 10;          // change 1
    while (number > 0) {   // change 2
        remainder = (int) (number % 10);
        sum = sum + getDigit(remainder * 2);
        number /= 100;
    }
    return sum;
}

public static int sumOfOddPlaces(long number) {
    int sum = 0;
    int remainder;

    while (number > 0) {   // change 3 
        remainder = (int) (number % 10);
        sum = sum + getDigit(remainder);
        number /= 100;
    }
    return sum;
}

Output :-

Enter Credit Card Number for Validation: 
 4246345689049834
4246345689049834 is invalid
anoopknr
  • 3,177
  • 2
  • 23
  • 33