# Luhn Algorithm(Mod10) Help (Beginner)

• March 28th, 2013, 01:15 PM
IBeeJava
Luhn Algorithm(Mod10) Help (Beginner)
Hi im trying to implement the Mod 10 as in the Luhn Algorith on a credit card number so it can validate if its a :/ ...valid account number.
HTML Code:

`http://www.codeproject.com/Tips/515367/Validate-credit-card-number-with-Mod-10-algorithm`
The above link shows what im doing, now all im doing is the validation procces im stuck on "Step 2". I know how to seperate a single double digit number and add them but i dont know where to put it can someone guide me the right direction? and it is not a assignment i just want to learn about methods and array and thought this would be a good thing to start with. Here is what i have so far.

Code :

```public class card   { public static void main(String[] args) {   //Just experimenting how to seperate a double digit int and add them? String s = "16"; String num1 = s.substring(0,1); String num2 = s.substring(1,2); int n1 = Integer.parseInt(num1); int n2 = Integer.parseInt(num2); int answer = n1+n2; System.out.println(answer); //^^^^^TEST! DONT KNOW WHERE TO PUT IT??? OR HOW TO USE IT   // My array(Credit Card number i will figure out at the end on how to get this array as user input) int cardNumb[] = {4,0,1,2,8,8,8,8,8,8,8,8,1,8,8,1};   validate(cardNumb);//Calling my method that multiplies every second value by two   for(int y: cardNumb)   System.out.print(" "+y);   } public static void validate(int x[] ){   for (int i = 0; i < 10; i+=2){   x[i]*=2;     }   } }```

X_X
• March 28th, 2013, 01:40 PM
Norm
Re: Luhn Algorithm(Mod10) Help (Beginner)
Quote:

i dont know where to put it
Can you explain the problem?
• March 28th, 2013, 01:56 PM
IBeeJava
Re: Luhn Algorithm(Mod10) Help (Beginner)
Well i am confused about the syntax, i don't know where that chunk of code is suppose to go (The part where it separates all single int with double digit values and adds them) so the outcome after i run the code i got 8 0 2 2 16 8 16 8 16 8 8 8 1 8 8 1 how do i make it 8 0 2 2 7 8 7 8 7 8 8 8 1 8 8 1 , so the double digits as in the 16 i separate 1 and 6 then add which is 7 i dont know where to apply that chunk, i am just really confused and sorry i am really bad t explaining things :S
• March 28th, 2013, 02:08 PM
Norm
Re: Luhn Algorithm(Mod10) Help (Beginner)
Quote:

double digits as in the 16 i separate 1 and 6 then add which is 7 i dont know where to apply that chunk
Is that explained in the algorithm that you are trying to code?
• April 1st, 2013, 08:48 AM
IBeeJava
Re: Luhn Algorithm(Mod10) Help (Beginner)
Quote:

Originally Posted by Norm
Is that explained in the algorithm that you are trying to code?

Nope, its from a random site and they are using C# i believe to implement the process. I do know its been used with java i'm just a bit confused as to how i split the double valued integers and add them inside the array and sorry for the late reply i haven't been online for a while
• April 1st, 2013, 09:12 AM
Norm
Re: Luhn Algorithm(Mod10) Help (Beginner)
Quote:

split the double valued integers
Is the variable a double or an int? They are different data types.
Or are you referring to int values in the range from 10 to 99?

The % and / operators can be used to get the separate digits from an int variable.