0

Hello I'm currently coding something for class. We are basically making a credit card checker to pull the numbers from a text file. The rules we have to follow for the check digit are the following.

  • Drop the last digit from the card number. The last digit is the check digit.

  • Reverse the digits.

  • Multiply the digits in odd positions (1, 3, 5, etc.) by 2.

  • Subtract 9 from any result higher than 9.

  • Sum all the digits.

  • 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)

So I pulled the check digit away by setting a new variable and taking the card # /10. It's in a long so no decimals so this gets rid of the last digit. I then stored that digit as my check digit using %10 of the original number. I then used a loop to reverse the digits which can be seen as:

                    long lcards = Long.parseLong(cards);                    
                    long lastDigit = lcards % 10;
                    long newCard = lcards / 10;

                    long reverseCard = 0;

                    while (newCard != 0)
                    {
                        reverseCard = reverseCard * 10;
                        reverseCard = reverseCard + (newCard % 10);
                        newCard = newCard / 10;
                    }

I'm now stuck on the next step :/. How would I do this? Thanks!

1 Answers1

0

Next step:

Multiply the digits in odd positions (1, 3, 5, etc.) by 2.

That requires you to iterate all digits in your input number. And there are two ways to do that:

  1. More or less the same as your first attempt to get rid of the last digit - you can use modulo/division operations to "access" each digit in your number in a similar way as you did before!
  2. Or, instead of working on one number, consider turning the whole number into an array of int values for example; like shown here. Now you can just iterate that array and make the necessary computations. And in the end, just "merge" the array back into a single number. You could even do that upfront, to get rid of the last digit.

Hope that helps to get you going on the rest of the exercise!

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • If I did it the first way though how would I know when to stop as I wouldn't actually know the length of the card number input seeing as mastercards for example can be 16-19 characters in length? – bobobobbins Feb 13 '17 at 19:11
  • I dont get your question. The link I gave you shows you how to turn **any** number into a sequence of values; no matter the length. And besides that; assume you do `long newCard = lcards / 10;` just think for a while how you could detect that you ran out of digits. Hint: worst case ... just try it, and print the values; and see what happens! – GhostCat Feb 13 '17 at 19:25
  • Sorry my question was regarding your first option of doing what I did to get rid of the last digit. I would have to create a new variable for each position though to do it that way, correct? If so how many positions would I know to create if I don't know the length of the long? – bobobobbins Feb 13 '17 at 19:31
  • Sorry, but SO doesn't work this way. We are not a free tutor service that guides you through your complete homework. Especially for "homework" kind of questions, we give hints; but not much more than that. So should get out of that ping/pong mode. But yes, you might have between 16 and 19 chars there ;-) ... that is why any reasonable solution becomes much easier when using arrays to represent that; even when you fill that array "manually" by only using a loop with division/modulo computations (instead of a number to string conversion). – GhostCat Feb 13 '17 at 19:34