I'm using a try/catch statement to force valid user input to assign to a variable. However, when I try to use this variable outside the try/catch statement, it tells me that my variable has not been initialized. Using Java... Link to picture of error
public static int getChoice()
{
//Variables
int choice;
do {
try {
System.out.println("Press 1 then [Enter] for imperial measurements: ");
System.out.println("Press 2 then [Enter] for metric measurements: ");
choice=console.nextInt();
}
catch (InputMismatchException inputMismatchException) { //Force valid input of integer
System.err.println("\nInvalid entry.");
System.out.println("Press 1 then [Enter] for imperial measurements: ");
System.out.println("Press 2 then [Enter] for metric measurements: ");
console.nextLine(); //Flush line buffer
}
} while (choice<1||choice>2); //Forces input of either 1 or 2
return choice;
}