# HELP! Luhn Algorithm Credit Card Validation

• December 26th, 2013, 11:09 AM
suzan
HELP! Luhn Algorithm Credit Card Validation
Hi Every One
Hope you're doing well !

any how

i'm really desperate i need a java program
the promotes the user to enter a Credit Card as a long number
then check for validation using Luhn Algorithm which i need to use methods for luhn rules .
I also have to take the long number of the credit card and assign it into an array after i apply
luhn rules in other methods .

hope you understand
and hope you heeeeeelp me pleeeeeeeeeeeeeeeeeas

Thanxx .
• December 26th, 2013, 11:14 AM
GregBrannon
Re: HELP! Luhn Algorithm Credit Card Validation
We understand, but what have you tried?
• December 26th, 2013, 11:54 AM
suzan
Re: HELP! Luhn Algorithm Credit Card Validation
i didn't know how to take the variable credit card number and assign it to an array after i'm finished ?

this is my code i took the credit card number as an array from the begging
and that's wrong .
i'm stuck :(

import java.util.Scanner ;
public class creditValidation {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long[] cardNmbers = new long [16];

do {
System.out.print("Enter a card number with 16 digit : ");
for (int i=0 ; i<cardNmbers.length ; i++ ){
cardNmbers[i] = input.nextInt();
multplyAndSum(cardNmbers[i]);
int summation = (int) (multplyAndSum(cardNmbers[i]) + addOdd(cardNmbers[i]));
System.out.println(cardNmbers[i] + "\n" + isTrue(cardNmbers[i],cardNmbers[i]));

}
}
while ((cardNmbers[0]!=4) ||(cardNmbers[0]!=5) || (cardNmbers[0]!=3 && cardNmbers[1]!=7) );
}

public static int multplyAndSum(long[] num1){
long num = long[]num1 ;
int sum = 0 , rem = 0 ;
while (num>0)
rem=rem%10;
num = num/10;
sum = sum * 10 + rem ;
System.out.println(num*2);
if (num>9){
num= num % 10 + num / 10;
}
sum +=num ;
return (int) sum ;
}

public static long addOdd (long [] oddNum){

int sum = 0;
int rem=0;
int count = 0 ;

while (oddNum>0){
oddNum=oddNum/10;
rem=rem%10;
count++;
if (count%2==1)
sum+=oddNum;

}
return sum;

}

public static boolean isTrue(long odd , long even){
multplyAndSum(even);
int summation = (int) (multplyAndSum(even) + addOdd(odd));
if (summation%10==0)
return true ;
else
return false ;
}

}

^ it's all wrong :'(
• December 26th, 2013, 12:27 PM
GregBrannon
Re: HELP! Luhn Algorithm Credit Card Validation
Read this FAQ for instructions on how to post code correctly and other useful tips for newcomers. Also, tell us what you think is wrong or what you'd like help with. Post errors, sample runs, whatever it takes to explain what's wrong with your code or why you're stuck and ask specific questions about what you don't understand that will help you move forward.

This sentence, "i didn't know how to take the variable credit card number and assign it to an array after i'm finished," doesn't make sense. You might look at the String API to get ideas on what can be done with String objects to accomplish what you're trying to do.