Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.util.NoSuchElementException error, looked for possible reasons, still can't fix it
    primarykey
    data
    text
    <p>I am getting java.util.NoSuchElementException error. We get this error for the following reasons.</p> <ol> <li>If we don't check if the file has next line before reading it, then it throws exception after reading last line since it is trying to read a line which doesn't exist.</li> <li>Format of the file is messed up</li> </ol> <p>I think that the format of the file I m using is correct and I am also checking for next line before reading it but I am still getting the error.</p> <p>When I debug it using print statement, it prints all the line and throws <code>java.util.NoSuchElementException</code> error after reading last line.</p> <p>Please help me out</p> <p>Here is the code :</p> <pre><code> public static void InterpretMessageFromFile() throws FileNotFoundException{ File inputfile = new File("filepath"); Scanner reader = new Scanner(inputfile); try { while (reader.hasNextLine()) { String MessageType = reader.next(); int IsAdd = MessageType.compareToIgnoreCase("A"); int IsCancel = MessageType.compareToIgnoreCase("X"); int IsDelete = MessageType.compareToIgnoreCase("D"); int IsExecute = MessageType.compareToIgnoreCase("E"); int IsReplace = MessageType.compareToIgnoreCase("U"); //if the type of order is add order to existing Order Book if (IsAdd == 0) { String retrieve_ts = reader.next(); int ts = Integer.parseInt(retrieve_ts); String retrieve_id = reader.next(); int id = Integer.parseInt(retrieve_id); String or_side = reader.next(); String retrieve_share = reader.next(); int share = Integer.parseInt(retrieve_share); String retrieve_price = reader.next(); int price = Integer.parseInt(retrieve_price); System.out.println("Add Order : Id is " + id ); AddOrderToExistingBook.AddNewOrder(id, ts, or_side, share, price); } //if it is cancel order if (IsCancel == 0){ String retrieve_ts = reader.next(); int ts = Integer.parseInt(retrieve_ts); String retrieve_id = reader.next(); int id = Integer.parseInt(retrieve_id); System.out.println("Cancel Order : Id is " + id + " time stamp is : " + ts ); CancelOrder.CancelPartOfOrder(id, ts); } } } } finally { reader.close(); } } </code></pre> <p><strong>Exception</strong> (copied from comments): </p> <blockquote> <p>Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at OrderBook.InterpretOrderBookUpdateMessage.InterpretMessageFromFile(InterpretOrde‌​rBookUpdateMessage.java:20) at OrderBook.MainMethod.main(MainMethod.java:50)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload