Search code examples
javastringvalidationcredit-card

Credit Card Long/String Validation


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;
    }
}

Solution

  • 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