So we are working on loops right now, I got the first portino of my program up and running how I want, took some messing around with but I finally got it. Here is my assignment:

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.

My main is running fine but here is what I have:

public class CheckSum {


    public static void main(String[] args) {
        Scanner numberInput = new Scanner(System.in);

        boolean valid = false;
        while(!valid){
        System.out.println("Please enter your credit card number for "
                + "verification: ");
        String ccNumber = numberInput.next();
        int ccNumberlength = ccNumber.length();
        if(ccNumberlength == 8) { valid = true; }
        else{ System.out.println("Incorrect number try again!");}
        }

    }
}

I wanted it to take in 8 digits only (what we are dealing with in the problem). Here is my problem, printDigit.java I don't know where too start, this is what I have:

public class CheckDigit {
    String cardNumber;

    public CheckDigit(String aCardNumber){
        String cardNumber = aCardNumber;

    }

    public void printDigits(){




    }
} 

I don't know what it is I am not getting. Thanks

Recommended Answers

All 9 Replies

what is it you're having trouble with? it's pretty well explained.

I guess I just don't understand what I have to do exactly (the algorithm) I'm trying to use some examples in my book and I can't get it. This is my first language and only experience. We get assignments and seems like there isn't much guidance. This is what I have now, I worked on it for a while longer and came up with this but get errors.

public class CheckDigit {
    String cardNumber;


    public CheckDigit(String aCardNumber){
        String cardNumber = aCardNumber;


    }
    public void printDigits(){
        int cardLength = cardNumber.length();
        for(cardLength = 8; cardLength < cardLength; cardLength-=2);{
        int sum = 0;
        while(cardLength <= 8 && cardLength >= 0){
            int input = cardLength;
            sum = sum + input;
            System.out.println(sum);
        }

      }           
    }
}  

When ran I get this:

Please enter your credit card number for verification: 
12345678
Exception in thread "main" java.lang.NullPointerException
    at checksum.CheckDigit.printDigits(CheckDigit.java:18)
    at checksum.CheckSum.main(CheckSum.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

Sorry this is like trying to learn a foreign language and I find it difficult. I feel I have learned quite a bit but I have troulbe understanding a lot still.

NullPointerException means you are using an instance of a class, which you haven't instantiated yet. it's still null (lack of value) .
on CheckDigit line 18 (for which I assume you have a different line than the one in the snippet above, since there isn't a variable there) you have an instance which is null. check which one and instantiate it before using it.

Ok I have it computing now, so I think I am getting closer, except I have a infinite loop. lol.

Here is what I have now:

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package checksum;



    public class CheckDigit {
        String cardNumber;

        public CheckDigit(String ccNumber){


        }
        public void printDigits(){
            int ccNumberLength;
            for(ccNumberLength = 1; ccNumberLength < 0; ccNumberLength-=2);{
            int sum = 0;
            while(ccNumberLength <= 8 && ccNumberLength >= 0){
                int input = ccNumberLength;
                sum = sum + input;
                System.out.println(sum);
            }

          }           
        }
    }  

It still tells me line 18 in CheckDigit is an empty statement. I don't know what I need to add. But think you stultuske, I somewhat fixed the problem. lol.

Java is parsing the last semicolon on line 18 as the statement that the for loop has to execute. Since it doen't contain any code (just a ;) that's what is called an empty statement. It also means the block that starts with the { on line18 is not part of the for loop. That semicolon is a mistake.

while(ccNumberLength <= 8 && ccNumberLength >= 0){
                int input = ccNumberLength;
                sum = sum + input;
                System.out.println(sum);
            }

here is your infinite loop. you never change the value for ccNumberLength, so if it's true the first time, it 'll be true forever.

for(ccNumberLength = 1; ccNumberLength < 0; ccNumberLength-=2);{

about that other problem: you have a semicolong right before the opening bracket of the for loop, which basically ends the for loop prematurely. just remove that.

Ok I got that fixed, no more errors. How do I get the last string digit to add to the third to last digit and so on ex:

im using 12345678 how do I get 8 + 6 + 4 + 2 = 20? Is the code I have even close to getting that?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checksum;



public class CheckDigit {
    int cardNumber;

    public CheckDigit(String ccNumber){
       cardNumber = ccNumber.length();

    }
    public void printDigits(){
            int ccNumberLength;
            for(ccNumberLength = cardNumber ; ccNumberLength < cardNumber; ccNumberLength-=2){
            int sum = 0;
                while(ccNumberLength == 8){
                int input = cardNumber;
                sum = sum + input;
                System.out.println(sum);
        }

      }           
    }
}  

Changed things around and probably longer than it needs to be, but I got most of it to work.

Here is what I did:

package checksum;



public class CheckDigit {
    int cardNumber;
    int digit1;
    int digit2;
    int digit3;
    int digit4;
    int digit5;
    int digit6;
    int digit7;
    int digit8;

    public CheckDigit(String ccNumber){
       cardNumber = ccNumber.length();
       digit1 = Integer.parseInt(ccNumber.substring(7,8));
       digit2 = Integer.parseInt(ccNumber.substring(5,6));
       digit3 = Integer.parseInt(ccNumber.substring(3,4));
       digit4 = Integer.parseInt(ccNumber.substring(1,2));
       digit5 = Integer.parseInt(ccNumber.substring(6,7));
       digit6 = Integer.parseInt(ccNumber.substring(4,5));
       digit7 = Integer.parseInt(ccNumber.substring(2,3));
       digit8 = Integer.parseInt(ccNumber.substring(0,1));

    }
    public void printDigits(){
        int sum1 = digit1 + digit2 + digit3 + digit4;
        int sum2 = (digit5 * 2) + (digit6 * 2) + (digit7 * 2) + (digit8 * 2);

        int total = sum1 + sum2;

            System.out.println(total);
        }
}      

But, what do I do if digits 5, 6, 7, or 8 are larger than 10 and I need to add the product numbers together. For example if I use 12345678 digit5 is 7 * 2 = 14, I need to split that into 1 + 4 = 5 and so on. If they are not greater than 10 I can leave them alone and just add them. So with those numbers I have it would technically be 14 + 10 + 6 + 2 = 32 but I need it converted to 1 + 4 + 1 + 0 + 6+ 2= 14. I have it to where the two sums are added 32 + 20 = 52, so almost there.

several ways you can do that.
let's say you have "12+5"
just create a new substring and perform (while next character is a digit, add nextcharacter to substring) and work with that?
I don't really know what you mean by

12345678 digit5 is 7 * 2 = 14, I need to split that into 1 + 4 = 5

so I'm not going to guess on that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.