nowbotcaster

Exception In Thread Main Java.util.nosuchelementexception

28.08.2019

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.

  1. Exception In Thread Main Java.util.nosuchelementexception Head Of Empty List
  2. Exception In Thread Main Java.util.nosuchelementexception Iterator
  3. Exception In Thread Main Java.util.nosuchelementexception In Selenium
  1. 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.
  2. 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

jww
56.2k4242 gold badges249249 silver badges540540 bronze badges
spataraspatara
44333 gold badges1212 silver badges2626 bronze badges

marked 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:

Hunter McMillenHunter McMillen
38.2k1717 gold badges8787 silver badges145145 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

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.

4castle
23k66 gold badges4848 silver badges7676 bronze badges
adaamException In Thread Main Java.util.nosuchelementexceptionadaam
3,03555 gold badges1818 silver badges4949 bronze badges

5 Answers

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

How about this :

user813853

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

AddictAddict
50022 gold badges66 silver badges1616 bronze badges

Exception In Thread Main Java.util.nosuchelementexception Head Of Empty List

Exception in thread main java.util.nosuchelementexception free

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 LiTerry Li
8,7902323 gold badges7070 silver badges122122 bronze badges

Integer#nextInt throws NoSuchElementException - if input is exhausted

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

Exception In Thread Main Java.util.nosuchelementexception Iterator

Subhrajyoti MajumderSubhrajyoti Majumder
34.9k1010 gold badges6565 silver badges9393 bronze badges

Exception In Thread Main Java.util.nosuchelementexception In Selenium

Kaushal Vyas 007Kaushal Vyas 007

Not the answer you're looking for? Browse other questions tagged javajava.util.scanner or ask your own question.

Post navigation

Ruger 1022 Stockr Screw
Drawn To Life Book Pdf

New Pages

    § Republic Commando Crash On New Game
    § Half Life 2 Barney Voice Actor
    § Ek Thi Naayka Watch Online
    § Dead Rising 2 Off The Record Coop
    § Download Narodna Srpska Muzika
    § Download Star Trek Bridge Commander
    § The Exorcist Full Movie In Hindi
    § Open Cities Fallout 4
    § Cara Update System Windows 7 Tidak Terdeteksi
    § Holder And Holder In Due Course
    § Index Of How To Train Your Dragon 3
    § How To Run Sven Coop Server
    § Mangal Font For Ms Word 2007 For Win 10
nowbotcaster