Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8188087

Scanner hasNext() does not recognize Ctrl-Z when Program is run in JShell

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "9"
      Java(TM) SE Runtime Environment (build 9+181)
      Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Ultimate SP1 x64
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      JAXB Tests with JShell.

      Start JShell: jshell JAVASE --add-modules java.se.ee



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      Scanner input = new Scanner(System.in);
      while (input.hasNext()) {
       ...
      }

      enter Ctrl-z
      is not recognized / does not end the while loop


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Ctrl z should end the while loop, that is
      input.hasNext() should return false
      ACTUAL -
      It waits for input

      Ctrl-d for example gives a NoSuchElementException

      Maybe JShell swallows Ctrl z

      jshell> TestCaseCtrl_z.runScanner()
      Enter number, first name and last name.
      Enter end-of-file indicator to end input.
      ? 100 Bernd Meier
      >>> Nr: 100
      >>> PreName: Bernd
      >>> Name: Meier

      ?


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // TestCase Ctrl-z

      import java.util.NoSuchElementException;
      import java.util.Scanner;

      public class TestCaseCtrl_z {

        public static void main(String[] args) {
          runScanner();
        }
        
        public static void runScanner() {
          Integer nr = new Integer(0) ;
          String preName = "";
          String name = "";
         
          try {
            Scanner input = new Scanner(System.in);
            
            System.out.printf("%s%n%s%n? ",
                "Enter number, first name and last name.",
                "Enter end-of-file indicator to end input.");

            while (input.hasNext()) {
            
              try {
                nr = new Integer(input.nextInt());
                preName = input.next();
                name = input.next();
              }
              catch (NoSuchElementException elementException) {
                System.err.println("Invalid input. Please try again.");
                input.nextLine(); // discard input so user can try again
              }

              System.out.println(">>> Nr: " + nr.toString());
              System.out.println(">>> PreName: " + preName);
              System.out.println(">>> Name: " + name);
              System.out.println("");
              System.out.println("? ");
            }
          }
          catch (Exception ex) {
            System.err.println("Caught Exception: " + ex.getMessage());
          }
        }
      }
      ---------- END SOURCE ----------

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: