vendredi 19 février 2021

nested if-statement not functioning properly

I have taken this from a program that checks for credit card validation. The code below is put inside another if-statement. Although the statement is true, it still outputs as it is false. Both "validation" and "lastDigit" are integers.

Here is my code:

                if (validation == lastDigit){
                    System.out.println("Check digit should be: " + validation);
                    System.out.println("Check digit is: " + lastDigit);
                    System.out.println("Number is valid.");
                    System.out.println();
                }
                else {
                    System.out.println("Check digit should be: " + validation);
                    System.out.println("Check digit is: " + lastDigit);
                    System.out.println("Number is not valid.");
                    System.out.println();
                }

Here is my output:

Enter a credit card number (enter a blank line to quit): 5457623898234113
Check digit should be: 3
Check digit is: 3
Number is not valid.

Enter a credit card number (enter a blank line to quit): 5555555555554445
Check digit should be: 4
Check digit is: 5
Number is not valid.

Enter a credit card number (enter a blank line to quit): 4012888888881881
Check digit should be: 1
Check digit is: 1
Number is not valid.

Enter a credit card number (enter a blank line to quit): 

Even when the validation and lastDigit does equal to each other, like the first and the third input, it still executes as false.

Am I missing something here?

Aucun commentaire:

Enregistrer un commentaire