Exception In Thread Main Java.util.nosuchelementexception

Exception In Thread Main Java.util.nosuchelementexception Rating: 7,2/10 2824 votes
  1. Java Scanner No Line Found
  2. Exception In Thread Main Java.util.nosuchelementexception At Java.util.scanner.throwfor(scanner.java

Exception in thread 'main' java.util.NoSuchElementException: No line found. I am writing a client/server application where the client writes a message that is sent to the server, and the server must print thi smessage on the screen. I changed the scanner into a buffered reader, it worked, but i want to know why the scanner doesnt work and how i can change the code to make it work. 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 (slight.

Thread

Java Scanner No Line Found

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.

A transaction is a unit of work that is performed against a database. It is important to control transactions to ensure data integrity and to handle database errors.Practically, you will club many SQL queries into a group and you will execute all of them together as a part of a transaction. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.A transaction is the propagation of one or more changes to the database. Sql server stored procedure example. For example, if you are creating a record or updating a record or deleting a record from the table, then you are performing a transaction on the table.

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

Exception In Thread Main Java.util.nosuchelementexception At Java.util.scanner.throwfor(scanner.java

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

Failed to write core dump. Core dumps have been disabled. To enable core dumping, try 'ulimit -c unlimited' before starting Java again

jww
56.2k42 gold badges249 silver badges540 bronze badges
spataraspatara
4433 gold badges12 silver badges26 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.2k17 gold badges87 silver badges145 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.