Question: I have to create a java program that verifies a credit card number based on whether it is a Visa or MasterCard. I am required

I have to create a java program that verifies a credit card number based on whether it is a Visa or MasterCard. I am required to read the card number as a single string with spaces in between each set of four digits (xxxx xxxx xxxx xxxx). There must be 16 digits. All the digits in the number must be totaled. Then, if sum%10=0, it is a valid Visa. If sum%10=1, it is a valid MasterCard. Any deviation from this results in an invalid message. My problem is that once I run my current program, I enter the number and the card type, and then the program stops and won't continue. I'm not sure what I'm doing wrong here.

import java.util.Scanner; public class Assignment4 { public static void main (String[] args) { String cardNum; String typeAnswer; char cardType; int testSum; int modResult;

Scanner scan = new Scanner (System.in);

System.out.println("\t\t Credit Card Verification"); System.out.println("\t\t ========================"); System.out.println("Enter your card number : "); cardNum = scan.nextLine();

if(cardNum.length()<19 || cardnum.length()>19) { System.out.println("Incorrect card number. Re-launch the program and enter a 16-digit card number"); System.exit(0); } else { System.out.println("Is your card Visa or MasterCard?"); typeAnswer = scan.next().toUpperCase(); cardType = answer.charAt(0);

String numSet1 = cardNum.substring(0,4); String numSet2 = cardNum.substring(5,9); String numSet3 = cardNum.substring(10,14); String numSet4 = cardNum.substring(15,19);

int i = Integer.parseInt(numSet1); int j = Integer.parseInt(numSet2); int k = Integer.parseInt(numSet3); int l = Integer.parseInt(numSet4);

int sum1=0; while(i>0) { sum1 = sum1 + (i%10); i = i/10; } int sum2 = 0; while(j>0) { sum2 = sum2 + (j%10); j = j/10; } int sum3 = 0; while(k>0) { sum3 = sum3+ (k%10); k = k/10; } int sum4 = 0; while(l>0) { sum4 = sum4 + (l%10); j = j/10; } //Testing clause

testSum = sum1 + sum2 + sum3 + sum4; modResult = testSum%10

if(modResult=0 && cardType=V) { System.out.println("Valid Visa card.");

} else if (modResult=1 && cardType=M) { System.out.println("Valid MasterCard."); } else { System.out.println("Not a valid " + typeAnswer + " card. Re-launch and try again."); }

}

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!