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

Thread: Credit Card String Program

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Credit Card String Program

    public class CreditCardNumber {
    private String issuerID = "000000";
    private String accountNum = "999999999";
    private int checkDigit;
     
    public CreditCardNumber(String tempIssuerID, String tempAccountNum) {
        if (tempIssuerID != null && tempAccountNum != null
                && tempIssuerID.length() == 6 && tempAccountNum.length() == 9)
            if (checkDigits(tempIssuerID) && checkDigits(tempAccountNum)) {
                issuerID = tempIssuerID;
                accountNum = tempAccountNum;
                calcCheckDigits();
            }
    }
    public CreditCardNumber() {
    	issuerID = "000000";
    	accountNum = "999999999";
    	//checkDigit = 0;
    }
     
    public boolean checkDigits(String temp) {
      char digit;
      for(int i = 0; i < temp.length(); i++){
    	  digit = temp.charAt(i);
    	  if(digit < '0')
    		  return false;
    	  if (digit > '9')
    		  return false;
      }
    	return true;			  
    }
     
    public String getIssuerID() {
        return issuerID;
    }
     
    public String getAccountNum() {
        return accountNum;
    }
     
    public int getCheckDigits() {
        return checkDigit;
    }
     
    private void calcCheckDigits() {
        int sum;
        sum = checkSum();
        if ((sum + checkDigit) % 10 == 0) {
            checkDigit = sum + (sum % 10); 
        }   
    }
     
    public void createCard(String tempIssuerID) {
    	if (tempIssuerID != null && tempIssuerID.length() == 6
                && checkDigits(tempIssuerID)) {
            issuerID = tempIssuerID;
        } else {
            issuerID = "000000";
        }
        StringBuilder tempString = new StringBuilder();
        for (int i = 0; i < 9; i++) {
            tempString = tempString.append((Math.random() * 9));     
        }
        accountNum = tempString.toString();
        calcCheckDigits();
    }
     
    private int checkSum() {
        StringBuilder temp = new StringBuilder();
        int num;
        int sum = 0;
     
       for(int i = 0 ; i < issuerID.length(); i++){
              temp = temp.append(issuerID.charAt(i));
              for(int j = 0 ; j < accountNum.length(); j++){            
            	  temp = temp.append(accountNum.charAt(j));
              }
          }
        for (int k = 0; k < temp.length(); k +=2) {
            num = temp.charAt(k);
            num *= 2;
            if (num >= 10)
                num = (num % 10) + 1;
            temp.setCharAt(k, (char)num);
            sum += num;
        } 
        return sum;
    }
     
    public String toString() {
    	String str = issuerID + accountNum + checkDigit;
    	return str;
        }
    }
    In another file:
     
    import java.util.Scanner;
     
    public class Prog4 {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args){   
    	CreditCardNumber[] objArray;
        CreditCardNumber objCred = getUserInput();
        displayCred("The complete number from your input:", objCred);
        objArray= getInputArray();
        displayArray(objArray);
        tryAnother(objArray);
    }
     
    public static CreditCardNumber getUserInput() {
    	String ID;
    	String accountNum;
    	CreditCardNumber userNum;      
        System.out.printf("Please enter issuer ID: ");
        ID = scanner.nextLine();
        System.out.printf("Please enter account number: ");
        accountNum = scanner.nextLine();
        userNum = new CreditCardNumber(ID, accountNum);
     
        return userNum;
    }
    public static void displayCred(String ch, CreditCardNumber cred){
     
        System.out.println(ch);
        System.out.println(cred.toString().replaceAll(".{4}", "$0 "));       
    }
     
    public static CreditCardNumber[] getInputArray(){
     
    	int size;   
        CreditCardNumber[] tempAry;
        String tempID;
        System.out.printf("Please enter size of the array: ");
        size = scanner.nextInt();
        if(size < 1) {
            size = 1;
        }
        tempAry = new CreditCardNumber[size];
        System.out.printf("Please enter issuer ID# (6 digits): ");
        tempID = scanner.next();
        for(int i = 0; i < tempAry.length; i++){
            tempAry[i] = new CreditCardNumber();
            tempAry[i].createCard(tempID);
        }
     
        return tempAry;
    }
     
    public static void displayArray(CreditCardNumber[] objArray){
        for(int i = 0; i < objArray.length; i++){
            displayCred("Credit Card # " + i + ":" + '\n', objArray[i]);
        }
        System.out.println();
    }
     
    public static boolean tryAnother(CreditCardNumber[] cred1) {
        String s;
        System.out.printf("Get more credit card numbers? (n for no):");
        s = scanner.next();
        if(s.charAt(0) != 'N' && s.charAt(0) != 'n'){                                       
                cred1 = getInputArray();
                displayArray(cred1);    
                if(s.charAt(0) != 'N' && s.charAt(0) != 'n')
                tryAnother(cred1);
        }       
     
        return false;   
    }
    }

    Desired output:
    Enter a credit card issuer number: 321321
    Enter an account number: 654654654
    The complete number from your input:
    3213 2165 4654 6549

    Enter the number of elements in the array: 1
    Enter an issuer ID# (6 digits): 789789

    Credit Card # 0:
    7897 8931 4062 1219

    However when I run my code I have two problems.
    1. For the complete number from input part, I my card number looks good except for the very last digit(its always a 0).
    I've tried editing multiple things to fix this but have had no luck.

    2. For my array printing credit card number, I just get a giant mess:
    Credit Card # 0:

    9879 878. 0511 2471 8666 2488 .625 2980 5398 0805 8.93 4724 9421 1246 52.1 0708 2984 6066 88.7 3952 9333 1195 260. 8686 9691 2926 3099 5.31 4072 8376 3121 .706 0624 4425 2281 48.3 2209 1522 6689 480

    -pretty much until the console runs out of room.

    Please help me out! I'm so close to finishing, and feel like this is probably something trivial that I am just not seeing.
    Thank you for your time and please let me know if you need additional details.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    the very last digit(its always a 0).
    Where is the code that creates that last digit? How have you tried to debug it so you'd understand why the digit is always 0? Add a println statement to print out the values of the variables used to create that last digit so you can see why it is 0.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    Hmmm its made from my checkSum method.... but I cannot understand how my sum could end up being zero. Everything looks good to me. I guess I'll try tinkering with it some more.

    --- Update ---

    Hmmm its made from my checkSum method.... but I cannot understand how my sum could end up being zero. Everything looks good to me. I guess I'll try tinkering with it some more.

    So it looks like it is checkSum. The sum that I am returning ends up being 194 no matter how many times I run the program. But I don't know how to fix this. Any ideas or suggestions?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    What prints out when the debugging println statements are executed?


    Can you provide all the input that the user must make to test the program?

    An easy way to make sure the program reads the right data is to preload the user's input by having a String for the Scanner class's constructor:
    static Scanner scanner = new Scanner("put user input here");
    Here's a sample:
     static Scanner scanner = new Scanner("123456789\n7\n88\n99\n766\n");
    Having the full input in the code makes sure all testing uses the same data AND makes testing much EASIER.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    My debugging println statement was used to print out the sum right before it is returned from the method chechSum. I provided the user input used in running the program in my very first post. Here it is just in case again:
    Enter a credit card issuer number: 321321
    Enter an account number: 654654654
    The complete number from your input:
    3213 2165 4654 6549

    Enter the number of elements in the array: 1
    Enter an issuer ID# (6 digits): 789789

    Credit Card # 0:
    7897 8931 4062 1219

    -This also has the correct output according to what the use typed in. Again, my current output doesn't really look like this. The specific couple of miss matches and errors I have were posted in my very first post.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    Can you change the Scanner definition as I showed above in post #4 so that it provides the answers to the questions that the program asks. With that change there will be no chance a user can enter the wrong data and will make sure that we test with the same data that you are testing with.

    Just post the Scanner definition to use, not the whole program.

    With that change to the Scanner definition, execute the code, copy the print out from the program here and add some comments to the print out that says what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    Okay here is my new scanner definition:
    static Scanner scanner = new Scanner("321321\n654654654\n2\n987987\nn");

    And my resulting output from it(still has the debugging println for sum):
    Please enter issuer ID: Please enter account number: Sum =194
    The complete number from your input:
    3213 2165 4654 6540 //last digit should be 9
    Please enter size of the array: Please enter issuer ID# (6 digits): Sum =2532
    Sum =2619
    Credit Card # 0: // obviously no credit card number can look like whats below. Its supposed to use an issuer Id provided by user in order to create random credit card numbers.

    9879 874. 1957 2020 0249 4630 .230 8979 5936 6537 686. 9324 5354 6494 0850 .580 6082 2567 8525 0.86 0127 2669 4060 165. 7792 1235 4782 9430 .589 5794 6431 3558 30.2 6711 0495 8269 1488 .865 4594 1953 6684 5.25 3117 3757 9904 10
    Credit Card # 1:

    9879 873. 9525 4553 0084 0986 .375 9792 9317 9106 4.49 9005 8484 7638 82.8 8416 4824 0961 9232 .818 6822 0966 4826 78.4 8417 5603 9336 641. 4951 9384 9440 5834 2.30 6253 8161 3130 782. 0691 3425 8678 8637 1.28 1691 4240 0613 830

    Get more credit card numbers? (n for no):

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    Where is the variable: checkDigit assigned a value? Is that line of code executed?
    To see, change this line:
       private int checkDigit = -1;                 //<<<<<<<<<<<
    and see what prints out

    Its supposed to use an issuer Id provided by user in order to create random credit card numbers.
    What is the output following Credit Card # 0: supposed to be?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    checkDigit is assigned inside of the method calcCheckDigits. But it relies on sum, which I'm pretty sure is wrong.
    If I assign chechDigit to -1 like you specified, the new output I get looks like this:
    The complete number from your input:
    3213 2165 4654 654- 1

    --- Update ---

    checkDigit is assigned inside of the method calcCheckDigits. But it relies on sum, which I'm pretty sure is wrong.
    If I assign chechDigit to -1 like you specified, the new output I get looks like this:
    The complete number from your input:
    3213 2165 4654 654- 1

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    3213 2165 4654 654- 1
    That means the code never assigns a value to checkDigit. Time for some debugging to see why it does not assign a value.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    I think it assigns a value, just not the right one because sum is completely off and too large... But I don't know where the logic in the code is wrong.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    I think it assigns a value
    It can NOT assign a value because it does not change the initial value of -1.
    Add a call to the println method (as suggest earlier in post#2) to print out a message with the values used to assign checkDigit a value when it is assigned a value to see what happens and why.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Credit Card String Program

    private int calcCheckDigits() {
        int sum;
        sum = checkSum();
        System.out.println("Sum2 = " + sum);
      //  if ((sum + checkDigit) % 10 == 0) {
            checkDigit += sum;
            System.out.println("checkDigit = " + checkDigit);
            return checkDigit;
        }

    I changed my checkDigit method to return checkDigit. The problem was with the line
    if ((sum + checkDigit) % 10 == 0) {
    checkDigit = sum + (sum % 10);
    }
    because that if statement wasn't true. I changed it to the one above, but now I have checkDigit being that same incorrect number as sum, (except one less since I originally set checkDigit to -1)

    And now my output is:
    3213 2165 4654 6541 93

    which is still obviously incorrect...

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Credit Card String Program

    which is still obviously incorrect...
    What should the output be?
    Are the formulas you are using to compute the checkdigit correct?
    Do you get the correct result doing the math manually?
    If not then ,maybe you need to get the correct formula and use that in the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help developing a basic credit program in Java
    By MuffinOfFun in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2012, 08:08 PM
  2. [SOLVED] Need help with Pick A Card Program Please
    By gcjava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2012, 04:46 AM
  3. Credit card help
    By kostas198 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2011, 10:50 PM
  4. Credit Card Problem (While and Switch will be used)
    By odun in forum Object Oriented Programming
    Replies: 3
    Last Post: March 29th, 2011, 12:54 AM
  5. Credit card validator
    By SOK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 03:11 AM