# Method for Cedit card check, help!!!!1

• October 23rd, 2009, 03:56 PM
raidcomputer
Method for Cedit card check, help!!!!1
2. Write a program to determine whether an input credit number is valid according to the Luhn check. Prompt the user for a credit card number as a long integer and display whether the number is valid.

Need help creating a method for this process, im unsure how to check a set of long numbers and iterate the steps. Please help me with.........

The Luhn check is performed as follows:

Step 1: Double every second digit from the right to the left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.

Step 2: Now add all single-digit numbers from Step 1.

Step 3: Add all the digits in the odd places from right to left in the card number.

Step 4: Sum the results from Step 2 and Step 3.

Step 5: If the result from Step 4 is divisible by 10, the card number is valid; otherwise it is invalid.
• October 23rd, 2009, 06:59 PM
helloworld922
Re: Method for Cedit card check, help!!!!1
What exactly do you need help with (what have you done)?
• October 24th, 2009, 02:11 PM
Json
Re: Method for Cedit card check, help!!!!1
I take it this is an assignment, however if its not, how about Validator - Commons Validator

// Json
• October 26th, 2009, 03:08 PM
raidcomputer
Im stuck on how to create methods for the sumofodd/even places method. What i have is below....:-t:-t

Code :

```import javax.swing.JOptionPane; import java.util.Scanner;   public class CreditCard{ public static void main(String [] args){   Scanner input = new Scanner(System.in);   System.out.println("Enter your credit card number: "); long number = input.nextLong();   System.out.println("Your credit card number is: " + number);   if (isValid(number)){ System.out.println("The credit card number\n " + number + " is a valid"); } else System.out.println("The credit card is not valid"); } public static boolean isValid(long number){ long sumOdd = sumOddPlaces (number); long sumEven = sumEvenPlaces (number);   if(sumOdd + sumEven % 10 == 0){ return true; } else{ return false; } }   public static int getDigit(int number){ if(number < 10) return number;   else{ int d2 = number % 10; int d1 = number / 10;   return d2 + d1; }   public static int sumOfEvenPlaces(long number){ String str_prime = (new Integer(number)).toString(); int lastIndex = str_prime.length()-1; for (i =0; i <= lastIndex - 2; i--){ if( )     }     public static in sumOfOddPlaces(long number){```

Here are the two methods i need names i need to make......

Code :

```// Get the result from Step 2   public static int sumOfEvenPlace(long number)   // Return the sum of the odd place digits in number   public static int sumOfOddPlace(long number)```
• October 27th, 2009, 04:23 AM
Json
Re: Method for Cedit card check, help!!!!1
My first question would be, can a credit card number start with a zero?

If so, you need to capture it as a string rather than a long.

Second of all I'd suggest you use a string anyway and loop through each caracter and extract ever odd one and sum them up.

// Json
• October 28th, 2009, 01:14 PM
raidcomputer
Re: Method for Cedit card check, help!!!!1
can you show me an example my professor has not covered that material yet...
• October 31st, 2009, 09:16 AM
literallyjer
Re: Method for Cedit card check, help!!!!1
Here's one way to see if a number is even or odd.

Code :

```if (n % 2 == 0) // n is even else // in is odd```