Hi all,
I'm having some issues with the Scanner not waiting for input from the keyboard.
The first method processChoice takes the userChoice and the idea is that in the switch statement there is a call to a method getUserInputForProcessing that takes the action and there scanner takes an input but it's not passing the control to the keyboard,
it goes directly to the Exception because nextLine is empty, not entirely sure why. Is that because it's inside a switch statement?
public void processChoice(int userChoice, BookManager bookManager) {
switch(userChoice) {
case 0:
System.out.println("Goodbye.");
System.exit(0);
break;
case 1:
System.out.println("You chose READ");
getUserInputForProcessing("READ");
bookManager.read();
break;
case 2:
System.out.println("You chose CREATE");
bookManager.create();
break;
case 3:
System.out.println("You chose UPDATE");
bookManager.update();
break;
case 4:
System.out.println("You chose DELETE");
getUserInputForProcessing("DELETE");
bookManager.delete();
break;
}
}
public String getUserInputForProcessing(String action)
{
Scanner scanner = new Scanner(System.in);
System.out.printf("You chose %s: ", action);
String nextLine = "";
if(action.equals(READ) || action.equals(DELETE) )
{
System.out.println("Enter the searching criterion");
}
try
{
nextLine = scanner.nextLine();
}
catch(Exception e)
{
e.printStackTrace();
}
scanner.close();
return nextLine;
}