How to end a file scanner while loop with undetermined length correctly in java
<p>I wrote a function that scans a tab delimited file of baseball stats.</p>

<pre><code>public static ArrayList dataRead() throws FileNotFoundException {
    //ArrayList array = new ArrayList&lt;ArrayList&gt;();
    Scanner s = new Scanner(new File("c:\\stats.txt")).useDelimiter("\r");

    ArrayList&lt;String&gt; array = new ArrayList&lt;String&gt;();
    int i = 0;
    while(s.next() != null) {
        String currentLine = s.next();
        Scanner split = new Scanner(currentLine).useDelimiter("\t");
        for(int j = 0; j &lt; 16; j++) {
            System.out.print(split.next() + " ");

            j++;

        }
        System.out.println("\r");
    }
    s.close();
    return array;
}
</code></pre>

<p>This function works until the end of the file. To my knowledge the while loop should close at the end of the file because it returns null, but I keep getting a noSuchElement error instead. If I could guarantee the file size, I would just use a for loop with the size, but I can't because team size could technically vary. How would I properly end the file scanner while loop so I can close the scanner?</p>
