Quoted from my assignment: The objectives of this question are (a) to practice selection structure (b) to apply iteration structure (c) to manipulate string
do {
System.out.print("Enter MC for MasterCard or VISA for VISA card: ");
cardType = scn.next();
} while (!cardType.equals("MC") && !cardType.equals("VISA"));
if (cardType.equals("MC")) {
do {
System.out.print("Enter MasterCard card no.: "); // Get input:
// mastercard
// number
cardNo = scn.nextLong();
cardLength = Long.toString(cardNo).length(); // Get length of
// mastercard
// number input
dbUserPrefix = cardNo / java.lang.Math.pow(10, 14);
intUserPrefix = (int) dbUserPrefix;
for (int i = 0; i <= 5; i++) {// for validating prefix
// 4 possibilities
if (intUserPrefix == cardPrefix[i]) {
if (cardLength == 16) { // Prefix & length correct break;
} else { // Prefix correct, length wrong
state = 1;
break;
}
} else {
if (cardLength == 16) { // Prefix wrong, length correct state = 2;
} else { // Prefix & length incorrect
state = 3;
}
}
}
if (state == 0) {
System.out.println("SUCESS");
} else if (state == 1) {
System.out.println("Your length of card number is incorrect.");
} else if (state == 2) {
System.out.println("Your card prefix is incorrect.");
} else {
System.out.println("Your card prefix and length of card number is incorrect.");
}
break;
} while (cardLength != 16);
}
The main thing I want here is the program to validate that the right Prefix of a credit card is 51,52,53,54 or 55. and the right length to be 16 (number of digits). If validation fails, the error must be printed out. Problem is that other than prefix==51, the rest of the prefix i tried results in state==2.