Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to create a java program that verifies a credit card number based on whet

ID: 3679680 • Letter: I

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 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(" Credit Card Verification");
       System.out.println(" ========================");
       System.out.println("Enter your card number <xxxx xxxx xxxx xxxx>: ");
       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.");
           }


       }


   }
}

Explanation / Answer

you made some sysntax mistake check comments where i corrected check it

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(" Credit Card Verification");
System.out.println(" ========================");
System.out.println("Enter your card number <xxxx xxxx xxxx xxxx>: ");
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 = typeAnswer.charAt(0); // here you wrote card type=answer.charAt(0) its wrong i made the change
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') // here its conditional statement that meeans you need to use "==" operator but you made it as assignment operator and you are checking for charecter v so it should be in'v'
{
System.out.println("Valid Visa card.");
}
else if (modResult==1 && cardType=='M') // here its conditional statement that meeans you need to use "==" operator but you made it as assignment operator and you are checking for charecter M so it should be in'V'
{
System.out.println("Valid MasterCard.");
}
else
{
System.out.println("Not a valid " + typeAnswer + " card. Re-launch and try again.");
}

}

}
}