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.

Re: No Such Element Exception - Please help! 80 80

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.

Re: No Such Element Exception - Please help! 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.