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 2 of 2

Thread: Credit card validator

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Credit card validator

    Hey All. I am trying to do a credit card validator. We can't use an array so this is what I have done so far. Not working for me unfortunately. I think my algorithm is off.

    import java.util.Scanner;
     
    public class creditCardValid
    {
        public static void main(String[] args)
        {
     
            Scanner input = new Scanner(System.in);
            int num = 0;
            int checkDigit;
            int digit = 0;
            int digit2 = 0;
            int digit16;
            int power = 1;
            int userInput = 0;
            int sum = 0;
            int totalSum = 0;
     
            System.out.print("Enter card number:");
     
            String cardNum = input.next();
     
            if (cardNum.length() < 13 || cardNum.length() > 16)
                {
                    System.out.println("Invalid card number: Out of range");
                }
             else
            {
     
                //calculates place value.
                checkDigit = num % 10;
                totalSum = checkDigit;
     
                while (power < 16)
                {
                    digit = num / (power * 10) % 10;
                    digit2 = num / (power * 100) % 10;
     
     
                    digit = (digit * 2) % 16;
                    sum = digit + digit2;
                    totalSum = totalSum + sum;
     
                    power = power * 100;
                }  
     
     
                digit16 = num / 16;
     
     
                if (totalSum % 10 != 0)
                {
                    System.out.println("Invalid card number: Fails check algorithm");
                }
                else
     
                {  
                    if (num == 51 || num == 52 || num == 53 || num == 54 || num == 55)
     
                        System.out.println("Valid MASTERCARD card number");
     
                    else if (num == 4)
     
                        System.out.println("Valid VISA card number");
     
                    else if (num == 34 || num == 37)
     
                        System.out.println("Valid AMEX card number ");
     
                    else if (num == 300 || num == 301 || num == 302 || num == 303 || num == 304 || num == 305 || num == 36 || num == 38)
     
                        System.out.println("Valid DINER CLUB card number");
     
                    else if (num == 6001)
     
                        System.out.println("Valid DISCOVER card number");
     
                    else if (num == 2014  || num == 2149)
     
                        System.out.println("Valid ENROUTE card number");
     
                    else if (num == 3)
     
                        System.out.println("Valid JCB card number");
     
                    else if (num == 1800 || num == 2131)
     
                        System.out.println("Valid JCB card number");
     
     
                }
     
              }      
        }
    }
    Last edited by SOK; October 14th, 2010 at 10:55 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Credit card validator

    Hello SOK,

    One of your problems is here:

    			// calculates place value.
    			checkDigit = num % 10;
    			totalSum = checkDigit;
     
    			while (power < 16) {
    				digit = num / (power * 10) % 10;
    				digit2 = num / (power * 100) % 10;
     
    				digit = (digit * 2) % 16;
    				sum = digit + digit2;
    				totalSum = totalSum + sum;
     
    				power = power * 100;
    			}
     
    			digit16 = num / 16;
     
    			System.out.println(totalSum);
     
    			if (totalSum % 10 != 0) {
    				System.out.println("Invalid card number: Fails check algorithm");
    			} else

    I added System.out.println(totalSum); so we could see the value. The totalSum value is always 0.

    This is why none of your if statements are executed.

    Also once cardNum is read in and the length checked, nothing else is done with it...

    Hope this helps
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2010, 10:37 AM
  2. my programs- Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2010, 08:06 PM
  3. Replies: 8
    Last Post: December 9th, 2009, 04:45 PM
  4. Credit and thrift society application(urgent)
    By 5723 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 3rd, 2009, 03:44 AM
  5. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM