I am running into an error with NoSuchElementException,

Please help!

Here is the error:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Project.answerKey(Project.java:292)
at Project.findMyAvg(Project.java:344)
at Project.printMyResults(Project.java:269)
at Project.printHeader(Project.java:203)
at Project.main(Project.java:30)

The code at line 292 is:

answer[count] = inScan.nextInt();

the method is:

public static int[] answerKey()
      int[] answer = new int[11];
      for (int count = 0; count <= 10; count++)
        answer[count] = inScan.nextInt();
      return answer;

The code at line 344 is

int[] answer = answerKey();

The code at line 269 is

System.out.println("Class Average (%) = " + findWHSAvg() + "%");

Please help me
Ask if you need to know more, please help.

5 Years
Discussion Span
Last Post by NormR1

The JavaDoc for NoSuchElementException is incredibly unhelpful

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

Obviously wrong, since you have no enums.

A look at the code for java.util.Scanner.throwFor tells us the real story:

// If we are at the end of input then NoSuchElement;
    // If there is still input left then InputMismatch
    private void throwFor() {
        skipped = false;
        if ((sourceClosed) && (position == buf.limit()))
            throw new NoSuchElementException();

So, your program is trying to get a nextInt when there input stream is closed and has nothing left to read.


Read the API doc for the Scanner methods you are using. There is a section of the doc that describes the exceptions a method throws and the reasons.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.