user1721664
user1721664

Reputation: 11

Credit Card validation - technically correct but error w/ algorithms?

So this is the assignment:

Credit Card Number Check using 8-digit numbers • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23. • Double each of the digits that were not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Add- ing all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27. • Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid. --valid/not valid?

I've created a tester program and a separate one for the methods. Everything successfully compiled so there shouldn't be any technical errors, but even when I enter in valid card numbers, the program returns that the number is invalid so I'm assuming there's something wrong with the actual algorithms but I'm pretty new at this so I can't figure out what it is.

import java.util.Scanner;

public class CreditCardTester{

public static void main(String[] args){

    Scanner scanner = new Scanner(System.in);
    String retry = ("y");
    String n = null;

    while(retry.equalsIgnoreCase("y")){ // allows program to keep running even if the user enters in a capital Y
        int lengthCheck = 1;
        System.out.println("Please enter your 8-digit credit card number");

    // check to see whether the input number is exactly 8 digits
        while(lengthCheck==1){
            n = scanner.next();
                if(n.length()==8)
                    lengthCheck=0;
                else
                    System.out.println("Please make sure the credit card number you have entered is 8 digits");
        } // end inner while loop

    // instantiate CardNumber and check validity
    CardNumber number = new CardNumber(n);
    number.check();
    if (number.isValid())
        System.out.println("The number you entered is a valid credit card number.");
    else
        System.out.println("The number you entered is not a valid credit card number. Would you like to enter in another number? (y/n)");
    retry = scanner.next();

    } // end outer while loop

}

}

and the separate class

public class CardNumber {

private String number;
private boolean valid;

public CardNumber(String n){
    number = n;
    valid = true;
}

private void check1(){
    int a = Integer.parseInt(number.substring(7));
    int b = Integer.parseInt(number.substring(5,6));
    int c = Integer.parseInt(number.substring(3,4));
    int d = Integer.parseInt(number.substring(1,2));

    int oddsum = a + b + c + d;

    int e = (Integer.parseInt(number.substring(6,7))) * 2;
    int f = (Integer.parseInt(number.substring(4,5))) * 2;
    int g = (Integer.parseInt(number.substring(2,3))) * 2;
    int h = (Integer.parseInt(number.substring(0,1))) * 2;

    String ee = String.valueOf(e);
    String ff = String.valueOf(f);
    String gg = String.valueOf(g);
    String hh = String.valueOf(h);

    int evensum = (Integer.parseInt(ee.substring(0))) + (Integer.parseInt(ff.substring(0))) + (Integer.parseInt(gg.substring(0))) + (Integer.parseInt(hh.substring(0)));

    int totalsum = evensum + oddsum;
    if (!(totalsum%10 == 0))
    valid=false;
}

public void check(){
    check1();
}

public boolean isValid(){
    return valid;
}

}

I'm sure there's also a much better way to do this, so all are suggestions appreciated!

Upvotes: 1

Views: 4865

Answers (3)

Supun Sameera
Supun Sameera

Reputation: 2703

You can find custom implantation of credit card validator here which is doing both credit card number validation plus credit card type detection,

http://www.esupu.com/credit-card-validator-java/

Upvotes: 0

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11950

Try this.

public class CardNumber {

    String number;
    boolean valid;

    public CardNumber(String n){
        number = n;
    }

    public void check(){
        // The odd sum
        // For the eight digits, 1, 3, 5, 7 are odd
        int a = Integer.parseInt("" + number.charAt(1));
        int b = Integer.parseInt("" + number.charAt(3));
        int c = Integer.parseInt("" + number.charAt(5));
        int d = Integer.parseInt("" + number.charAt(7));

        int oddsum = a+b+c+d;

        // The even sum
        int e = (Integer.parseInt(number.substring(6,7))) * 2;
        int f = (Integer.parseInt(number.substring(4,5))) * 2;
        int g = (Integer.parseInt(number.substring(2,3))) * 2;
        int h = (Integer.parseInt(number.substring(0,1))) * 2;

        // As suggested by RUP to make it more simple
        int evensum = e + f + g + h;

        // Total sum
        int totalsum = oddsum + evensum;
        valid = (totalsum%10==0)?true:false;            
    }

    public boolean isValid(){
        return valid;
    }

}

Upvotes: 0

Majid Laissi
Majid Laissi

Reputation: 19788

The credit card number validation is called the Luhn Algorithm. Here's a java implementaion http://www.xinotes.org/notes/note/595/

For your code I think here:

 int evensum = (Integer.parseInt(ee.substring(0))) + (Integer.parseInt(ff.substring(0))) + (Integer.parseInt(gg.substring(0))) + (Integer.parseInt(hh.substring(0)));

You mean to sum both digist:

int evensum = Integer.parseInt(ee.substring(0,1)) +  Integer.parseInt(ee.substring(1)) ... 

Upvotes: 1

Related Questions