"Line not available" Error When Reading nextLine(); From a File Scanner
<p>I am trying to write a program that reads a number of files and compares them line by line. The number of files to be read are taken from the user, and the files are expected to be in the form "input1.txt", "input2.txt" and so on.</p>

<p>When I try to run my code I get a "NoSuchElementException" telling me that Scanner.nextLine() line: line not avaialble. This is at line 50, which is: <code>jthLine.add(myScanners.get(k - 1).nextLine());</code>. I can't figure out why there wouldn't be a line to read as this is already done in a loop that is bounded by the minimum number of lines.</p>

<p>Any help is appreciated! Here is my code:</p>

<pre class="lang-java prettyprint-override"><code>// Compares n input files, prints the lines that are not equal in all.

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class CompareFiles {
public static void main(String[] args) throws IOException {
    Scanner consoleScanner = new Scanner(System.in);
    int numberOfFiles;
    System.out.println("Please enter how many files are there. The" +
            "file names should be like: input1.txt input2.txt, etc.");
    numberOfFiles = consoleScanner.nextInt();

    ArrayList&lt;Scanner&gt; myScanners = new ArrayList&lt;Scanner&gt;();

    for (int k = 1; k &lt;= numberOfFiles; k++)
        myScanners.add(new Scanner(new File("input" + k + ".txt")));

    // Find the file line counts.
    ArrayList&lt;Integer&gt; lineCounts = new ArrayList&lt;Integer&gt;();
    Integer lineCount;
    String increment;

    for (int i = 1; i &lt;= numberOfFiles; i++) {
        lineCount = 0;

        while (myScanners.get(i - 1).hasNext()) {
            increment = myScanners.get(i - 1).nextLine();
            lineCount++;
        }

        lineCounts.add(lineCount);
    }

    // Find the minimum file count.
    int minLineCount = Collections.min(lineCounts);

    // Compare files until minLineCount line by line
    // println all the unmatching lines from all files
    // jthLine holds the incremented lines from all files
    ArrayList&lt;String&gt; jthLine = new ArrayList&lt;String&gt;();
    boolean allMenAreTheSame;

    for (int j = 1; j &lt;= minLineCount; j++) {
        allMenAreTheSame = true; // are all jth lines the same?

        for (int k = 1; k &lt;= numberOfFiles; k++) {
            jthLine.add(myScanners.get(k - 1).nextLine());

            if (!jthLine.get(0).equals(jthLine.get(k)))
                allMenAreTheSame = false;
        }

        if (!allMenAreTheSame) {
            System.out.println();
            System.out.println("Line number " + j + " is not the same" + 
            "across all files, printing the lines:");

            for (int k = 1; k &lt;= numberOfFiles; k++) {
                System.out.println("File: \"input" + k + ".txt\":");
                System.out.println(jthLine.get(k - 1));
            }
        }

        jthLine.clear();
    }
}
</code></pre>

<p>}</p>
