18

I am very new to Java but am working through the book Java: How to program (9th ed.) and have reached an example where for the life of me I cannot figure out what the problem is.

Here is a (slightly) augmented version of the source code example in the textbook:

import java.util.Scanner;
public class Addition {
  public static void main(String[] args) {
    // creates a scanner to obtain input from a command window

    Scanner input = new Scanner(System.in);

    int number1; // first number to add
    int number2; // second number to add
    int sum; // sum of 1 & 2

    System.out.print("Enter First Integer: "); // prompt
    number1 = input.nextInt(); // reads first number inputted by user

    System.out.print("Enter Second Integer: "); // prompt 2 
    number2 = input.nextInt(); // reads second number from user

    sum = number1 + number2; // addition takes place, then stores the total of the two numbers in sum

    System.out.printf( "Sum is %d\n", sum ); // displays the sum on screen
  } // end method main
} // end class Addition

I am getting the 'NoSuchElementException' 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 Addition.main(Addition.java:16)
Enter First Integer:

I understand that this is probably due to something in the source code that is incompatible with the Scanner class from java.util, but I really can't get any further than this in terms of deducing what the problem is.

4castle
  • 28,713
  • 8
  • 60
  • 94
adaam
  • 3,616
  • 5
  • 24
  • 51
  • What SDK are you using? I just pasted your code into Eclipse, and it seems to work fine! – Curlystraw Dec 05 '12 at 17:54
  • I am running the program from within TextMate on Mac OS X, although when I run the program from Terminal, I get no errors. So I think it is an environmental issue rather than anything to do with the source code – adaam Dec 05 '12 at 19:37
  • 1
    @adaam The reason you got this error is because TextMate terminal simulator can't (or not properly configured to) accept inputs, I got the same issue in Sublime Text 2. – AsyncMoksha Feb 07 '14 at 23:42

8 Answers8

8

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

http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html

How about this :

if(input.hasNextInt() )
     number1 = input.nextInt(); // if there is another number  
else 
     number1 = 0; // nothing added in the input 
  • I receive "variable number1 might not have been initialized" and the same for the second variable. – adaam Dec 05 '12 at 18:00
  • that means you have not initialized the variable `number1`. try `int number1 = 0; int number2 = 0; ` –  Dec 05 '12 at 19:13
2

You should use hasNextInt() before assigning value to variable.

Addict
  • 693
  • 2
  • 7
  • 16
2

NoSuchElementException will be thrown if no more tokens are available. This is caused by invoking nextInt() without checking if there's any integer available. To prevent it from happening, you may consider using hasNextInt() to check if any more tokens are available.

Terry Li
  • 14,856
  • 25
  • 82
  • 131
1

You must add input.close() at the end...

1

This error is mostly occur in case of 0nline IDE's on which you are testing your code. It is not configured properly, as if you run the same code on any other IDE/Notepad it works properly because the online IDE is not designed such a way that it will adjust the input code of your format, So you have to take input as the Online IDE supports.

Deepanshu
  • 71
  • 6
1

I faced this Error with nextDouble(), when I input numbers such as 5.3, 23.8 ... I think that was from my PC depending on computer settings that use Arabic (23,33 instead 23.33), I fixed it with add: Scanner scanner = new Scanner(System.in).useLocale(Locale.US);

0

Integer#nextInt throws NoSuchElementException - if input is exhausted

You should check if there is a next line with Integer#hasNextLine

if(sc.hasNextLine()){
    number1=sc.nextInt();
}
Subhrajyoti Majumder
  • 38,572
  • 11
  • 72
  • 100
  • Just a sidenote, (I use Mac OS X) I have been trying to compile and run this source code from within my text editor (TextMate- which allows for this kind of procedure). But I have been experiencing the NoSuchElementException still despite your suggestions. When I javac and java the file inside of Terminal however, the example above runs perfectly - why is this? – adaam Dec 05 '12 at 19:35
0

If I may, I solved this issue today by realizing that I had multiple functions that used an instance of a Scanner, each. So basically, try refactoring so that you have only one instance opened and then closed in the end - this should work.