Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Luhn Algorithm validation code

807600Nov 21 2007 — edited Nov 21 2007
I REALLY need help with this code:

Here's how the Luhn Algorithm works:

*1.* Starting with the rightmost digit (which is the check digit) and moving left, double the value of every second digit. For any digits that thus become 10 or more, add the digits together. For example, 8762 becomes 7732 (the second digit from the right is 6, 2 ? 6 = 12, and 1 + 2 = 3, so 6 is replaced by 3; the fourth digit from the right is 8, 2 ? 8 = 16, and 1 + 6 = 7, so 8 is replaced by 7).

*2.* Add all the digits together (both the unchanged odd-positioned digits and the changed even-positioned digits). For example, since 8762 becomes 7732, we obtain 7 + 7 + 3 + 2 = 19.

*3.* If the total ends in 0 then the number is valid according to the Luhn algorithm; else it is not valid. So, 7732 is not valid (as just shown, it produces 19, which does not end in 0).

Array e of ints contains the digits of a number without its check digit. Write code to calculate the check digit for this number and store it in checkdigit. The check digit should be such that, if it were inserted after the last digit of e, the result would be the digit array of a number that is valid according to the Luhn algorithm.
int[] e = { 1, 3, 2, 1 };
         int checkdigit;
         int sum = 0;
         int dd;

for ( int i = 0; i < e.length; i++ )
{
 if {indexOf.e[i] % 2 == 0)
dd += (e[i] * 2)
 {
  if (dd == 10)
 dd = 1;
  if (dd == 11)
 dd = 2;
  if (dd == 12)
 dd = 3;
  if (dd == 13)
 dd = 4;
  if (dd == 14)
 dd = 5;
  if (dd == 15)
 dd = 6;
  if (dd == 16)
 dd = 7;
  if (dd == 17)
 dd = 8;
  if (dd == 18)
 dd = 9;
 }
  if (e.indexOf % 2 != 0)
sum += e[i];
}
checkdigit = * ;
Can someone please help me figure out how to find the checkdigit?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2007
Added on Nov 21 2007
18 comments
2,696 views