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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Method for Cedit card check, help!!!!1

  1. #1
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Method for Cedit card check, help!!!!1

    What exactly do you need help with (what have you done)?

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  4. #4
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Im stuck on how to create methods for the sumofodd/even places method. What i have is below....

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

    // 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)
    Last edited by helloworld922; October 26th, 2009 at 03:15 PM.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  6. #6
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Re: Method for Cedit card check, help!!!!1

    can you show me an example my professor has not covered that material yet...

  7. #7
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Method for Cedit card check, help!!!!1

    Here's one way to see if a number is even or odd.

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

Similar Threads

  1. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  2. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  3. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  4. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM
  5. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 04:37 AM