Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java.util.NoSuchElementException error

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default java.util.NoSuchElementException error

    I have this output :

    [1, 2, 3, 4, 5, 6, 7, 8]
    Enter a number : 4
    3
    Enter a number : Exception in thread "main" java.util.NoSuchElementException
            at java.base/java.util.Scanner.throwFor(Scanner.java:937)
            at java.base/java.util.Scanner.next(Scanner.java:1594)
            at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
            at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
            at NumberElementsSubArray.main(NumberElementsSubArray.java:36)

    This is a portion of my code where I think the problem comes from :

    while (true) {
                try (Scanner input = new Scanner(System.in)) {
                    System.out.print("Enter a number : ");
                    int userInput = input.nextInt();
                    if (userInput == 0)
     
                        break;
                    if (userInput != 0)
                        System.out.println(getNumberElementSubArray(userInput, numbers)); // print the number of elements
                                                                                          // of
                                                                                          // subArray
                }
            }

    Can you explain me this error? And how to fix it?
    It is weird because earlier after a lot of test case I didn't have any issue & it did run for more than 2 user input. Now this comes up right after one user input.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.util.NoSuchElementException error

    One problem I see is that a new Scanner object is created each time the loop iterates. Using a try with resources closes the System.in at the end of the containing block. System.in can only be closed ONE time in a program's execution. Trying to use it a second time causes the problem.
    Move the statement that creates the new Scanner with System.in outside of the while loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    siid14 (December 24th, 2022)

  4. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: java.util.NoSuchElementException error

    Indeed. It worked. I put Scanner object out of the loop and put a input.close; instead of using try resource.

    --- Update ---

    so @Norm -- what is the issue when "a new Scanner object is created each time the loop iterates" ?

    (I did understand the issue about the use of try with resources)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.util.NoSuchElementException error

    what is the issue ...
    The issue is with closing System.in. It can only be closed one time in a program.
    I think that once it is closed, it can never be opened again in the same program.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    siid14 (December 24th, 2022)

Similar Threads

  1. Replies: 6
    Last Post: August 16th, 2014, 01:34 AM
  2. In JSP i am Getting java.util.NoSuchElementException
    By anjijava16 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 12th, 2014, 04:52 AM
  3. Replies: 4
    Last Post: April 16th, 2013, 06:50 AM
  4. java.util.NoSuchElementException
    By xSedated in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2013, 06:03 AM
  5. Replies: 11
    Last Post: August 30th, 2012, 02:30 PM

Tags for this Thread