I have a method
getIntInput()
which returns selection made by a user when called. so now my question is how can I validate user input to be of certain range of option say like 1,2,3,4,5 only anything less or more an exception will be thrown say invalid selection and returns to the top to ask again.I know this can be achieved using a while or do while but how am I going to go about it.
public static int getIntInput(String prompt){
Scanner input = new Scanner(System.in);
int choice = 0;
System.out.print(prompt);
System.out.flush();
try{
choice = input.nextInt();
}catch(InputMismatchException e){
System.out.print("Error only numeric are allowed");
getIntInput(prompt);
}
return choice;
}