0

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;
}}
sakshi
  • 23
  • 4
  • 1
    Use only one scanner per source, not many. and replace nextInt with `Integer.parseInt(userInput.nextLine())` – azro Oct 06 '22 at 18:35
  • From where / what is the exception thrown? Can you edit your question to point to the line? – Old Dog Programmer Oct 06 '22 at 18:44
  • 1
    Don't try to use more than one `Scanner` for a single input source (i.e, the keyboard in your example). Open it once, _outside_ of and _before_ the loop. Close it once, outside of and _after_ the loop. – Old Dog Programmer Oct 06 '22 at 18:46
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Old Dog Programmer Oct 06 '22 at 18:52

1 Answers1

0

Not sure why you are creating multiple scanners and closing them inside a while loop. Create one scanner outside of your while loop for user input and make sure to close it when the user enters '0'.

userInput.nextInt() will not consume the carriage return and will remain in the scanner until you consume it with userInput.nextLine();

int input = userInput.nextInt();
userInput.nextLine(); // consumes the carriage return from scanner

You could also parse the scanner for the Integer value.

int input = Integer.parseInt(userInput.nextLine());

Modified your code a bit:

   Scanner userInput = new Scanner(System.in);  // Create one Scanner object
   boolean running = true;
   Stack<String> stack = new Stack<>();
        
   while (running) {
      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");
            
    /* int input = Integer.parseInt(userInput.nextLine()); // Parse the integer value*/
    int input = userInput.nextInt(); // doesn't consume the carriage return
    userInput.nextLine(); // Consume the next line in scanner to clear the carriage return
            
   switch(input) {
      case 0:  {
         userInput.close();
         System.out.println("Terminating program");
         running = false;
         break;
      }
      case 1 : {
         System.out.println("Enter string to be pushed onto stack");
         String userString = userInput.nextLine();
         stack.push(userString);
         System.out.println(userString + " has been pushed onto the stack");
         break;
      }
      default : break;
   }
}
userInput.close(); // close your resources
Dennis LLopis
  • 315
  • 2
  • 6