Can someone please highlight to me the problem With my main method? I am getting the error exception that scanner is closed once I complete first option and try to enter another?
I think the problem I am having is from the placement of my try catch and finally blocks but not totally sure! Thanks!
/**
* Scanner used for input within program
*/
public static Scanner scanner = new Scanner(System.in);
/**
* Main method that provides user with a menu in which each number
* represents a different task in which they can carry out
*/
public static void main(String[] args) {
int menuOption=0;
do{
try {
// Declaring var for user Input (defaulted to 0)
menuOption = showMenu();
switch (menuOption) {
case 1:
add();
break;
case 2:
subtract();
break;
case 3:
generateRandomNumber();
guessRandomNumber();
break;
case 4: // invoke print loop method (use params here to get
// experience!)
break;
case 5: // invoke print sum and average
break;
case 6: System.out.println("Quitting Program...");
break;
default:
System.out.println("Sorry, please enter valid Option");
}// End of switch statement
} catch (Exception ex) {
//flush scanner
scanner.next();
}
finally {
// Finally block ensures scanner is always closed
scanner.close();
}
}while(menuOption!=6);
//Exiting message
System.out.println("Thanks for using this Program...");