Welcome to Kodlogs Q&A, where you can ask programming questions and receive answers from other members of the community. For programmers, by programmers Kodlogs is an open community for anyone that does coding. We help you get answers to your toughest coding questions, share knowledge with your coworkers in private, and find your next IT dream job.
0 votes
7 views

How To Fix ‘Exception In Thread “Main” Java.Util.NoSuchElementException’ ?

Following is the error i am getting when running my program , please help.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ClassGrades.main(ClassGrades.java:24) 

in Java Programming by (2.5k points)
edited by

5 Answers

0 votes

The issue happens on the grounds that you close the scanner occasion and this additionally shuts the hidden stream. So your System.in stream is shut. 

Simply test it yourself with straightforward code: 

Java Code: 

import java.util.Scanner; 

open class JavaTests { 

open static void main(String[] args) { 

attempt (scanner = new Scanner(System.in)) 

String line = scanner.next(); 

attempt (scanner = new Scanner(System.in)) 

String line = scanner.next(); 

by (8.5k points)
0 votes

Four fixes for “Main” Java.Util.NoSuchElementException’ 

first Fix 

As was referenced previously, this special case blunder message for the most part results from a large number of causes. 

In light of oracle.com, it is basic that you should utilize a Scanner when attempting to fix these sorts of mistakes. Recorded underneath are significant strides in guaranteeing you have not kept away from these. 

While utilizing Scanner, the client must check if there is a next line with hasNextLine(). 

Likewise, remember that it is reliant on whether the information is appropriately organized. 

second Fix 

As indicated , this is an exceptionally normal and normal blunder case. This case ordinarily happens when a Java application will in general repeat over an unfilled Set. So as to keep this mistake from occurring, a Java application should call the hasNext first. 

third Fix: 

The NoSuchElementException extends the time of the RuntimeException class and thus has a place with the special cases which can be tossed during the Java Virtual Machine (JVM) task. A RuntimeException class is viewed as the superclass of these special cases that can be usually tossed when under the ordinary task procedure of the JVM. It's an unchecked special case, and it shouldn't be declared in a constructor's or a strategy's tosses condition. 

fourth fix (Preventive) 

This aides in staying away from the special case to be tossed into the code. Pursue the two stages recorded beneath: 

On the off chance that a software engineer isn't sure about the quantity of ints in his or her record, don't attempt to store them into a fixed-size exhibit. Attempt to use ArrayList. 

Try not to utilize perpetual circle while(true) however consider utilizing Input.hasNext() to look at if there still is something to peruse from a record.

by (8.5k points)
0 votes

 try this:

  1. InputStreamReader isr = new InputStreamReader(System.in);
  2. BufferedReader br = new BufferedReader(isr);
by (8.5k points)
0 votes
The most effective method to fix java.util.NoSuchElementException in Java

java.util.NoSuchElementException is a RuntimeException which can be tossed by various classes in Java like Iterator, Enumerator, Scanner or StringTokenizer. Those classes has strategy to get next component or next tokens if fundamental information structure doesn't have any component Java tosses "java.util.NoSuchElementException". Most regular case of this emphasizing over hashmap without checking if there is any component or not and that is the reason it's encouraged to utilize hashNext() before calling straightaway() on Iterator. In this Java instructional exercise we will what makes NoSuchElementException in Java and how evade it totally.

Peruse more: https://javarevisited.blogspot.com/2012/02/how-to-tackle javautilnosuchelementexcep.html#ixzz5uvLr8The

Reason for Exception in string "principle" java.util.NoSuchElementException:

The most effective method to understand java.util.NoSuchElementException in Javahere are conceivable reason for java.util.NoSuchElementException in Java:

1) according to Javadoc NoSuchElementException is tossed in the event that you call nextElement() technique for Enumeration and there is

no greater component in Enumeration. beneath code will toss java.util.NoSuchElementException since Enumeration of hastable is unfilled.

Peruse more: https://javarevisited.blogspot.com/2012/02/how-to-understand javautilnosuchelementexcep.html#ixzz5uvLxWSX0
by (8.5k points)
0 votes

The NoSuchElementException in Java 

The NoSuchElementException can be tossed by the accompanying strategies: 

Enumeration::nextElement() 

NamingEnumeration::next() 

StringTokenizer::nextElement() 

Iterator::next() 

All the previously mentioned strategies attempt to restore the following component of a list and toss that special case, so as to demonstrate that no more components exist. 

The most effective method to manage the NoSuchElementException 

An exceptionally basic mistake case is the point at which a Java application attempts to repeat over a vacant Set. So as to maintain a strategic distance from this blunder, a Java application should call the hasNext or hasMoreElements or StringTokenizer techniques

by (8.5k points)
...