When running it catches exceptions properly but when i choose a a case it causes an infinite loop. I have tried for hours looking for a solution and nothing has helped.
int whatYouWant = 1;
boolean contin = true;
while (whatYouWant != 0) {
while (contin) {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Which program would you like to run?" + " Input 0 to exit the program.");
whatYouWant = scanner.nextInt();
contin = false;
scanner.close();
} catch (Exception e) {
System.out.println("Try again.");
contin = true;
}
}
AllCode a = new AllCode();
switch (whatYouWant) {
case 1:
// method call that calls a method with another try catch block
break;
case 2:
// another method call
break;
case 3:
// another method call and so on and so forth
break;
case 0:
whatYouWant = 0;
break;
default:
System.out.println("Please try again.");
break;
}
}
Here is an example of a method that the switch calls:
void multiplytheNumbers() {// Using the parameter to
boolean run = true;
while (run) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the 1st number:");
int num1 = sc.nextInt(); /// getting user input from the user.
System.out.println("Enter the 2nd number:");
int num2 = sc.nextInt(); /// getting more user input
int product = num1 * num2; /// using the multiplication operator
int sum = num1 + num2; /// using then addition operator
int diff = num1 - num1; /// using the subtraction operator
int quotient = num1 / num1; /// using the quotient operator
System.out.println(sum);
System.out.println(diff);
System.out.println(product);
System.out.println(quotient);
num1 = num1++; // using the increment operator to increase num1
// by 1
num2 = num2--; // using the decrement operator to decrease num2
// by 1
run = false;
sc.close();
} catch (Exception e) {
System.out.println("Invalid Input. Try again.");
}
}
}