I am attempting to write a credit card validator. I can not, how ever, figure out for the life of me why the perfectly valid credit cards I'm inputting are coming up invalid.
I'm using the Luhn equation for validation.
The Luhn method is as follows:
The Luhn Formula:
- Drop the last digit from the number. The last digit is what we want to check against
- Reverse the numbers.
- Multiply the digits in odd positions (1, 3, 5, etc.) by 2 and subtract 9 to all any result higher than 9.
- Add all the numbers together The check digit (the last number of
the card) is the amount that you would need to add to get a
multiple of 10 (Modulo 10)
Credit cards examples are coming from this website, where I also got the equation from.
My code is here, on Gist.
Thanks!
(Also, this is my first time with Javadoc! Let me knwo how I did!)
EDIT: The Luhn method I'm using that apparently doesn't work:
/**
* Runs the Luhn Equation on a user inputed CCN, which in turn
* determines if it is a valid card number.
* @param c A user inputed CCN.
* @param cn The check number for the card.
* @return If the card is valid based on the Luhn Equation.
*/
public boolean luhn (String c, char cn)
{
String card = c;
//Drop the last digit.
card = card.substring(0, ( card.length() - 1 ) );
//Reverse the digits.
String cardrev = new StringBuilder(card).reverse().toString();
//Store it in an int array.
char[] cardArray = cardrev.toCharArray();
int[] cardWorking = new int[cardArray.length];
int addedNumbers = 0;
for (int i = 0; i < cardArray.length; i++)
{
cardWorking[i] = Character.getNumericValue( cardArray[i] );
}
//Double odd digits (which are really even in our case, since index starts at 0).
for (int j = 0; j < cardWorking.length; j++)
{
if ( j % 2) == 0)
{
cardWorking[j] = cardWorking[j] * 2;
}
}
//Subtract 9 from digits larger than 9.
for (int k = 0; k < cardWorking.length; k++)
{
if (cardWorking[k] > 9)
{
cardWorking[k] = cardWorking[k] - 9;
}
}
//Add all the numbers together.
for (int l = 0; l < cardWorking.length; l++)
{
addedNumbers += cardWorking[l];
}
//Finally, check if the number we got from adding all the other numbers
//when divided by ten has a remainder equal to the check number.
if (addedNumbers % 10 == cn)
{
return true;
}
else
{
return false;
}
}
EDIT 2:
I think the problem is here, but I'm not sure why. addedNumbers
is 50, and cn
is 0, but it's still returning false.
if (addedNumbers % 10 == cn)
{
//Debug
System.out.println(addedNumbers + " % 10 = " + cn);
return true;
}
else
{
//Debug
System.out.println(addedNumbers + " % 10 =/= " + cn);
return false;
}
My output in console:
>4024007142327070
707232417004204
[7, 0, 7, 2, 3, 2, 4, 1, 7, 0, 0, 4, 2, 0, 4]
[14, 0, 14, 2, 6, 2, 8, 1, 14, 0, 0, 4, 4, 0, 8]
[5, 0, 5, 2, 6, 2, 8, 1, 5, 0, 0, 4, 4, 0, 8]
50
50 % 10 =/= 0
You inputted card: 4024007142327070
Your card is not valid because it doesn't check against the Luhn Equation.
50 % 10 =/= 0
Well that's not right. What's going on here?
Quick Edit.
I was so mad, I checked.
50 modulo 10 is in fact, 0.