Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to use <code>\r\n</code> as the delimiter:</p> <pre><code>.useDelimiter("\r\n"); </code></pre> <p><strong>Update:</strong></p> <p><a href="http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNextLine%28%29" rel="nofollow">hasNextLine()</a></p> <blockquote> <p>Returns true if there is another line in the input of this scanner.</p> </blockquote> <p><a href="http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#next%28%29" rel="nofollow">next()</a></p> <blockquote> <p>Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.</p> </blockquote> <p>So, basically what's happening is that for the last line <code>hasNextLine()</code> is returning true but somehow the last line does not seem to end with <code>\n</code> so it's not a complete token, which why <code>next()</code> is throwing <code>NoSuchElementException</code>.</p> <p>You seem to be trying read the file line by line (as you are using <code>\n</code> as the delimiter for the <code>next()</code> method and <code>hasNextLine()</code> in your condition). So, I would recommend trying getting rid of the delimiter and using <a href="http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextLine%28%29" rel="nofollow">nextLine()</a> instead of <code>next()</code>:</p> <pre><code>scanner.nextLine(); </code></pre> <p>Which would basically return the line disregarding whatever the line actually ends with.</p>
 

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