Results 1 to 3 of 3
  1. #1
    bluegti02 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Question Credit Card Validator

    ok well i have to write a credit card validator

    Java Code:
    import java.util.*; // for the Scanner class
    
    public class CCValidator
    {
        public static void main (String [] args)
        {
            Scanner sc = new Scanner(System.in);
        
          
            System.out.print("Please enter a number to validate: ");
            long input = sc.nextLong();
        
            
            boolean isValid = numberIsValid(input);
        
    
            System.out.print("This credit card number ");
            if (isValid)
            {
                System.out.print("is ");
            }
            else
            {
                System.out.print("is NOT ");
            }
    
            System.out.println("valid.\n");
        }
    
        public static boolean numberIsValid (long num)
        {
            int checksum = 0;
                    for ( int place = 0; place < 16; place++ )
                        {
                        int digit = (int) ( num % 10 );
                        num /= 10;
                        if ( ( place & 1 ) == 0 )
                            {
                       
                            checksum += digit;
                            }
                        else
                            {
                            checksum += ( digit );
                            }
                        if ( num == 0 )
                            {
                            break;
                            }
                        }
    
                    return ( checksum % 10 ) == 0;
                    }
              
     
            
           
            
            
    
        }



    these are the instructions given to us

    One validation algorithm is the LUHN formula. This formula works as follows:

    1. Set the sum to 0
    2. Add up the digits in the credit card number, working from right to left:
    * If the digit has an odd position, counting from the right, add it to the sum as-is. Thus, the first, third, etc. digits (counting from the right) are all added directly to the sum.
    * Otherwise, double the value of the current digit. If it is over 10, add the digits to get a number less than 10. Then add the result to the sum.
    3. If the sum divides evenly by 10 (i.e., with a remainder of 0), then the credit card number is valid. Otherwise, the card number is invalid.

    For example, consider the (abbreviated and fictional) card number 499673. Doubling alternate digits (4, 9, 7), we get: (4 * 2) + 9 + (9 * 2) + 6 + (7 * 2) + 3

    (9 * 2) and (7 * 2) are greater than 10, so we add the digits: (9 * 2) = 18 = (1 + 8) = 9. Doing the same for 7, we get 5. HINT: you can also just subtract 9 from the doubled digit.

    The sum of these digits is 40, which divides evenly by 10, so this number is valid.
    and well im terribly stuck its my 2nd maybe 3rd week of classes and afraid of falling behind if any1 can offer help or a place to look for some advice that would be wonderful

  2. #2
    bluegti02 is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default

    close this thread please

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. [SOLVED] File I/O Extra Credit Assignment FTW!
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 06-10-2008, 09:20 AM
  2. Getting network card address in Java 6
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 08:10 PM
  3. a little help needed for card drawing
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 01-09-2008, 04:40 AM
  4. Card program , need help thanks.
    By carlos123 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:23 AM
  5. Replies: 1
    Last Post: 07-06-2007, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •