Exception In Thread Main Java.util.nosuchelementexception
Exception in thread 'main' java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Dragon.main(Dragon.java:81) I have tried hasNextLine and hasNextInt, and when I use while hasNextLine in the main method, I get a ton more errors.
- Exception In Thread Main Java.util.nosuchelementexception Head Of Empty List
- Exception In Thread Main Java.util.nosuchelementexception Iterator
- Exception In Thread Main Java.util.nosuchelementexception In Selenium
- Exception in thread 'main' java.util.NoSuchElementException: You're thinking correctly. If you're unsure how to use the pearls smohd has given you, look at the Scanner API, Scanner.hasNext in particular, look for examples where Scanner is used to read from a file (they should all use hasNext ), and/or check out the Java file I/O tutorials.
- Feb 24, 2012 java.util.NoSuchElementException is a RuntimeException which can be thrown by different classes in Java like Iterator, Enumerator, Scanner or StringTokenizer. All of those classes has method to fetch next element or next tokens if underlying data-structure doesn't have any element Java throws 'java.util.NoSuchElementException '.
This question already has an answer here:
- How to use java.util.Scanner to correctly read user input from System.in and act on it? 1 answer
I am trying to use Scanner to get an int from the keyboard, but I getting the following error:
This is what I have. It is independent of the rest of my program, I don't understand why this isn't working. It is declared in a method that is being called in a while loop, if that helps.
I stepped through with the debugger and narrowed the error down to:
A fatal error has been detected by the Java Runtime Environment: SIGSEGV (0xb) at pc=0xb6bdc8a8, pid=5587, tid=1828186944
JRE version: 7.0_07-b30 Java VM: OpenJDK Server VM (23.2-b09 mixed mode linux-x86 ) Problematic frame: V [libjvm.so+0x4258a8] java_lang_String::utf8_length(oopDesc*)+0x58 Cancellations due to weather.
Failed to write core dump. Core dumps have been disabled. To enable core dumping, try 'ulimit -c unlimited' before starting Java again
jwwmarked as duplicate by user177800 Sep 10 '17 at 6:54
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1 Answer
You should use the hasNextXXXX()
methods from the Scanner
class to make sure that there is an integer ready to be read.
The problem is you are called nextInt()
which reads the next integer from the stream that the Scanner
object points to, if there is no integer there to read (i.e. if the input is exhausted then you will see that NoSuchElementException
)
From the JavaDocs, the nextInt()
method will throw these exceptions under these conditions:
- InputMismatchException - if the next token does not match the Integer regular expression, or is out of range
- NoSuchElementException - if input is exhausted
- IllegalStateException - if this scanner is closed
You can fix this easily using the hasNextInt()
method:
Not the answer you're looking for? Browse other questions tagged javajava.util.scanner or ask your own question.
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:
I am getting the 'NoSuchElementException' error:
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.
5 Answers
NoSuchElementException
Thrown by the nextElement
method of an Enumeration to indicate that there are no more elements in the enumeration.
How about this :
You should use hasNextInt()
before assigning value to variable.
Exception In Thread Main Java.util.nosuchelementexception Head Of Empty List
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.
Integer#nextInt
throws NoSuchElementException
- if input is exhausted
You should check if there is a next line with Integer#hasNextLine