The first int scanner works and outputs "Enter string to be pushed onto stack" as is expected, however the strong scanner does not work. As soon as the user presses enter for the first scanner, and the first string is printed, it throws a NoSuchElementException immediately, how do I take in the user's input?
while (true) {
System.out.println("1. Place string on stack"
+ "\n"
+ "2. Remove top string on stack"
+ "\n"
+ "3. Examine top string on stack"
+ "\n"
+ "4. See if stack is empty"
+ "\n"
+ "5. Find size of stack"
+ "\n"
+ "0. Quit");
Scanner userInput = new Scanner(System.in); // Create a Scanner object
int input = userInput.nextInt();
userInput.close();
switch(input) {
//user quits program
case 0: System.exit(0);
break;
//user wants to place a string on the stack
case 1:
Scanner userInputString = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter string to be pushed onto stack");
String userString = userInputString.nextLine();
userInputString.close();
stack.push(userString);
System.out.println(userString + " has been pushed onto the stack");
break;
}}