CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Unhappy Credit Card Checker

    Hi I'm new to this forum and I was looking for help on my program for homework. Here is the problem:

    Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

    Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23.

    Double each of the digits that were not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.

    Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid.

    Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print out the value of the check digit that would make the number valid.
    Here are sample program runs:

    Enter 8 digit credit card number: 43589794
    The credit card number is not valid.
    The last digit should be 5

    Enter 8 digit credit card number: 43589795
    The credit card number is valid.
    Your main class should be called CreditCardCheck.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------

    I'm getting an error on the line that says lastdig = Integer.substring(total.length()-1, total.length()); and it is: int cannot be dereferenced. Here is the work I have done so far:
    Code:
    package creditcardcheck;
    import java.util.Scanner;
    
    public class CreditCardCheck {
        
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int cardNumber = in.nextInt();
            int sum1=0;
            int temp1 = cardNumber;
            int total = 0;
            String strd = "";
            int lastdig;
    
            for (int i=1;i<8;i+=2)
            {
                sum1+= temp1%10;
                temp1/= 100;  
            }
            
            int sum2=0;
            int temp2= cardNumber / 10;  //start with d2 not d1
            for (int i=1; i<8; i+=2)
            {
                int digit = (temp2 % 10) *2; // don't forget to double the value- thus the *2 here
                sum2 += digit%10;  //the ones digit
                digit /= 10;  //the tens digit 
                sum2 += digit;  
                temp2 /= 100; // move left two digits, base, ten, to get to the next relevant digit...
            }
            total = sum1+sum2;
            strd = Integer.toString(total);
           lastdig = Integer.substring(total.length()-1, total.length());
            
            if (lastdig == 0)
            {
                System.out.println("The credit card number is valid");
            }
            else 
            {
                System.out.println("The credit card number is not valid. The last digit should be " + ((cardNumber - (sum1+sum2) + 10) % 10));
            }
        }
    }
    Please help ASAP!! Thanks in advance.

  2. #2
    Join Date
    Sep 2013
    Location
    Chennai
    Posts
    5

    Re: Credit Card Checker

    Replace the following line..

    lastdig = Integer.substring(total.length()-1, total.length());

    to

    lastdig = Integer.parseInt(strd.substring(strd.length()-1, strd.length()));



    The error will be solved and get the answer..

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured