I am currently working on a program that requires answers to various math equations. I have the program working fine with displaying correct and incorrect based of the inputted numerical value. I am trying to add an extra aspect to the code that detects an incorrect input, and prompts the user that their input is in the incorrect format (ex. if the user inputs the letter a, the program displays "Your answer does not meet the correct format, make sure your answer is an integer"). I tried doing this through if statements but equal to (==) and not equal to (!=) only takes care of the numerical aspect. How do I go about this form of error detection in my code? Thanks for the help!
Here is my current code (I know its messy):
Random random = new Random();
int a = random.nextInt(10);
int b = random.nextInt(10);
int c = random.nextInt(4);
int d = 0;
int e = 0;
int f = 0;
Scanner input = new Scanner(System.in);
System.out.println("Would you like to choose your math problems, or random problems?");
System.out.println("Input 'randomize' for random problems and 'choose' to choose your operator.");
Scanner inputproblem = new Scanner(System.in);
f = inputproblem.nextInt();
int randomize = 1;
int choose = 2;
if(f == 1){
System.out.println("What operator type would you like? (add,subtract,multiply,divide)");
Scanner operatortype = new Scanner(System.in);
int add = 0;
int subtract = 1;
int multiply = 2;
int divide = 3;
int g = operatortype.nextInt();
if (g == 0){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" + ");
d=a+b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 1){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" - ");
d=a-b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 2){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" * ");
d=a*b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
else if (g == 3){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
System.out.print(" / ");
d=a/b;
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}
}
else if (f == 2){
System.out.print("\t\t\t\tWhat is ");
System.out.print(a);
//Switch that allows for the random generation of different math operators
switch (c){
case 0:
System.out.print(" + ");
d=a+b;
break;
case 1:
System.out.print(" - ");
d=a-b;
break;
case 2:
System.out.print(" / ");
d=a/b;
break;
case 3:
System.out.print(" * ");
d=a*b;
break;
}
System.out.print(b+"?");
e = input.nextInt();
input.close();
if (e == d){
System.out.print("\t\t\t\tCorrect!");
}
else if(e != d){
System.out.println("\t\t\t\tIncorrect.");
System.out.println("\t\t\t\tThe correct answer is: "+d);
}