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

NoSuchElementException thrown after closing a Scanner that's tied to System.in

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8, 11, 16, 18
    • Fix Version/s: None
    • Component/s: core-libs

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10/JDK 16

      A DESCRIPTION OF THE PROBLEM :
      When I open a Scanner object after I have close another Scanner object declared in another function, method nextInt throws NoSuchElementException

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the functions
          static void k5_14() {
              int i;
              int sum=0;
              Scanner in=new Scanner(System.in);
              do {
                  System.out.print("Enter int (0 terminates input):");
                  i=in.nextInt();
                  sum+=i;
              } while (i!=0);
              in.close();
              System.out.println("Sum = "+sum);
          }
          
          static void k5_22() {
            int sum=0;
            Scanner input=new Scanner(System.in);
            for (int i=1;i!=0;) {
                System.out.print("Enter int (0 terminates input):");
                i=input.nextInt();
                sum+=i;
            }
            input.close();
           System.out.println("Sum = "+sum);
          }
      like this

      k5_22();
      System.out.println("-----------------------------------");
      k5_14();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The sum of a series of integers given by the user
      ACTUAL -
      NoSuchElementException

      ---------- BEGIN SOURCE ----------
      import java.util.Scanner;

      public class Tmp {
          static void k5_14() {
              int i;
              int sum=0;
              Scanner in=new Scanner(System.in);
              do {
                  System.out.print("Enter int (0 terminates input):");
                  i=in.nextInt();
                  sum+=i;
              } while (i!=0);
              in.close();
              System.out.println("Sum = "+sum);
          }
          
          static void k5_22() {
            int sum=0;
            Scanner input=new Scanner(System.in);
            for (int i=1;i!=0;) {
                System.out.print("Enter int (0 terminates input):");
                i=input.nextInt();
                sum+=i;
            }
            input.close();
            System.out.println("Sum = "+sum);
          }
          
          public static void main(String[] args) {
              k5_14();
              k5_22();
          }
          
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            igraves Ian Graves
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: