Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Luhn Algorithm(Mod10) Help (Beginner)

1. ## 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.

```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);
//^^^^^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;

}

}
}```

2. ## Re: Luhn Algorithm(Mod10) Help (Beginner)

i dont know where to put it
Can you explain the problem?

3. ## 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

4. ## Re: Luhn Algorithm(Mod10) Help (Beginner)

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?

5. ## Re: Luhn Algorithm(Mod10) Help (Beginner)

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

6. ## Re: Luhn Algorithm(Mod10) Help (Beginner)

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.