adaam
adaam

Reputation: 3706

NoSuchElementException with Java.Util.Scanner

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.

Upvotes: 22

Views: 132169

Answers (10)

Utkarshthgr8
Utkarshthgr8

Reputation: 1

I added a single static scanner (sc) at the top of my class and closed it (sc.close()) when coming out of the whole class wherever I used return statements. Again that's one instance of scanner as suggested by another answer, which should be static.

package com.example.com;
import java.util.Scanner;

public class someClass {
    static Scanner sc = new Scanner(System.in);
    //Whole world of methods using same sc.
    //sc.close()); return;
}

Other than that you can add @SuppressWarnings("resource") on the top of the troubling method to make the warning go away. But be careful about resource leaks.

Upvotes: 0

douglas
douglas

Reputation: 116

For anyone using gradle's application plugin, you must wire it to the standard console in build.gradle(.kts) otherwise it will keep throwing the NoSuchElementException error if you try to use scanner.

For groovy:

run {
standardInput = System.in}

For gradle kotlin dsl:

tasks.withType<JavaExec>() {
standardInput = System.`in`}

Upvotes: 2

Saeed Dai Alnoor
Saeed Dai Alnoor

Reputation: 25

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);

Upvotes: 2

maganoegi
maganoegi

Reputation: 31

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.

Upvotes: 1

Deepanshu
Deepanshu

Reputation: 179

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.

Upvotes: 1

Kaushal Vyas 007
Kaushal Vyas 007

Reputation: 11

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

Upvotes: 1

user813853
user813853

Reputation:

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 

Upvotes: 12

Terry Li
Terry Li

Reputation: 17268

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.

Upvotes: 2

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

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();
}

Upvotes: 0

Addict
Addict

Reputation: 823

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

Upvotes: 3

Related Questions