简体   繁体   中英

how to generate a random numbers for a credit card

I created a code to generate a random credit card number and I'm using this method to return a credit card number based in the user input. For some reason I am having a problem returning a value. Help please.

 public String getIssuerCode(String issuerSymbol) {
        String creditCardNumber = null;

        for (int i = 0; i < 15; i++) {

            switch (issuerSymbol) {
                case ISSUER_MASTER_CARD:

                  creditCardNumber   = generateMasterCard();
                    break;
                case ISSUER_AMER_EXPRESS:
             creditCardNumber   = generateAmericanExpress();
                    break;
                case ISSUER_VISA:
                 creditCardNumber   = generateVisa();
                    break;
                // System.out.println("error");
                default:
                    break;
            }

        }

        return creditCardNumber;
    }

You have a couple of things going on here.

First and foremost, you have a simple typo in your code, as @brycem in his answer already told you. Secondly, it is possible that, after the for-loop and switch statements, creditCardNumber hasn't been initialized. This means that no value has been assigned to it. Now this may be impossible to happen, but your compiler doesn't know that. Thus, a simple fix is to assign it to null in the beginning:

public String getIssuerCode(String issuerSymbol) {
    String creditCardNumber = null;

    for (int i = 0; i < 15; i++) {

        switch (issuerSymbol) {
            case ISSUER_MASTER_CARD :
                creditCardNumber = generateMasterCard();
                break;
            case ISSUER_AMER_EXPRESS :
                creditCardNumber = generateAmericanExpress();
                break;
            case ISSUER_VISA:
                creditCardNumber = generateVisa();
                break;
            default:
                break;
        }

    }

    return creditCardNumber;
}

Edit:

From your comment below I drew the conclusion that the class that has this method also has the issuerNumber. In this case, you should definitely do this:

public String getIssuerCode() {
    String creditCardNumber = null;

    for (int i = 0; i < 15; i++) {

        switch (this.getIssuerSymbol()) {
            case ISSUER_MASTER_CARD :
                creditCardNumber = generateMasterCard();
                break;
            case ISSUER_AMER_EXPRESS :
                creditCardNumber = generateAmericanExpress();
                break;
            case ISSUER_VISA:
                creditCardNumber = generateVisa();
                break;
            default:
                break;
        }

    }

    return creditCardNumber;
}

The problem in your case is that there is one path in your program that might return an uninitialized String. If "issuerSymbol" is none of the three cases, you will fall in your "default case", which don't initialize your creditCardNumber variable.

The solution is to give a default value to the variable creditCardNumber at line 2:

public String getIssuerCode(String issuerSymbol) {
         String creditCardNumber = null;
         ...
}

OR update your default case

       switch (issuerSymbol) {
            case ISSUER_MASTER_CARD :
             creditCradNumber = generateMasterCard();
                break;
            case ISSUER_AMER_EXPRESS :
               creditCardNumber = generateAmericanExpress();
                break;
            case ISSUER_VISA:
               creditCardNumber = generateVisa();
                break;
            default:
               creditCardNumber = "000-000-000";
               break;
        }

Feel free to put any value as the default value. Personnally "null" is a good candidate as it shows immediately that something wrong happened, but it might make you crash later on if don't handle it correctly. Therefore a safer choice could be any String value, like "000-000-000".

Yeah, well. In case you didn't notice by now, you simply mispelled "card" and wrote "crad". Concretely, you created a variable String creditCradNumber; <- Notice the "Crad" instead of "Card".

But you spelled it differently in the return statement return creditCardNumber; <- NOW it's Card.

That's most likely your problem. Take another look at your code now and be sure to check your variable's name spelling now so it remains the same every time you want to use it. :)

Cheers.

Edit: As shown in other answers, your code ALSO has the problem of not initializing the String creditCardNumber if none of the cases are met on your switch. Initializing it to null:

String creditCardNumber = null;

or using a default case on your switch:

default:
    creditCardNumber="000-000-000";
    break;

may solve the problem.

在多个实例中进行设置时,您使用的变量名为“ creditCradNumber”,而不是“ creditCardNumber”。

Okay, just to debug your "null", you should try to add more debug lines:

 public String getIssuerCode(String issuerSymbol) {
    String creditCardNumber = null;

    System.out.println(issuerSymbol);

    switch (issuerSymbol) {
        case ISSUER_MASTER_CARD:
            System.out.println("Card was of type: " + ISSUER_MASTER_CARD);
            creditCardNumber = generateMasterCard();
            break;
        case ISSUER_AMER_EXPRESS:
             System.out.println("Card was of type: " + ISSUER_AMER_EXPRESS);
             creditCardNumber = generateAmericanExpress();
             break;
        case ISSUER_VISA:
             System.out.println("Card was of type: " + ISSUER_VISA);
             creditCardNumber = generateVisa();
             break;
        default:
             System.out.println("Card is NOT one of the expected type!");
             break;
        }
    }

    System.out.println("Generated credit card number is : " + creditCardNumber);

    return creditCardNumber;
}

If you have "null", then you must see "Card is NOT one of the expected type!". If not, this means that there is a problem in your function generatedAmericanExpress() and this is what returns null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM