• Register
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.

Most popular tags

java program php python javascript c# android r c spring-boot arrays html mysql entity-framework sql-server matlab redirect code program numpy spring asp loop net class quirks-mode internet-explorer-9 internet-explorer machine-learning winforms visual-studio-2010 webpack python-3 python-2 aspnet dataframe pandas jdbc ios eclipse sum loops css cmd tcp math security jquery sockets sql 7 number firebase-authentication aws-organizations amazon-web-services php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys sql-server-2005 windows-services react-redux reactjs inputstream facebook-graph-api for-loop entity-framework-4 reportingservices-2005 reporting-services mips linear-regression scikit-learn anova deep-learning keras block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx indentation-error eclipseide javafx-2 php-not-recognized laravel command-line unit-testing tinyurl atom-editor android-emulator android-studio android-sdk-tools ionic2 cordova jupyter-notebook foobar2k tcplistener net-2 net-4 ole-db-provider windows-10 vagrantfile ggplot2 glmnet jvm-arguments global-variables orm virtualenv atom xls oledb redux webclient prediction headless hyperlink outlook pipeline iis perfect-square objective-c carthage xcode8 compiler-errors numpy-ndarray standard-deviation xampp apple mdf destructor vagrant gettime gmail ioexception heuristics milliseconds reporting cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jtextfield jpanel inputmismatchexception deque jupyter javafx lvalue junit tensorflow ibm factorial servlet apache boot virtualbox indentation jvm margins 2147483647 xcode firebase plot deployment processor automation socketexception pointers concatenation oracle devices color recursion sequence search date caching expected response directory csv algorithms release collections facebook float figure url expression microsoft sorting sort datetime httpwebrequest cryptography json ajax exe dictionary required windows string variable size dll system exception files x int function random import code 2
0 votes
12 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) 

by (1.5k points)  
reshown 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.9k 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.9k points)  
0 votes

 try this:

  1. InputStreamReader isr = new InputStreamReader(System.in);
  2. BufferedReader br = new BufferedReader(isr);
by (8.9k 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.9k 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.9k points)  
...