1

いくつかのファイルを読み取り、それらを1行ずつ比較するプログラムを作成しようとしています。読み取るファイルの数はユーザーから取得され、ファイルは「input1.txt」、「input2.txt」などの形式であることが期待されます。

コードを実行しようとすると、「NoSuchElementException」が発生し、Scanner.nextLine()行:行が使用できないことを通知します。これは50行目です。これは次のとおりjthLine.add(myScanners.get(k - 1).nextLine());です。最小行数で制限されたループですでに実行されているため、読み取る行がない理由がわかりません。

どんな助けでも大歓迎です!これが私のコードです:

// 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<Scanner> myScanners = new ArrayList<Scanner>();

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

    // Find the file line counts.
    ArrayList<Integer> lineCounts = new ArrayList<Integer>();
    Integer lineCount;
    String increment;

    for (int i = 1; i <= 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<String> jthLine = new ArrayList<String>();
    boolean allMenAreTheSame;

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

        for (int k = 1; k <= 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 <= numberOfFiles; k++) {
                System.out.println("File: \"input" + k + ".txt\":");
                System.out.println(jthLine.get(k - 1));
            }
        }

        jthLine.clear();
    }
}

}

4

1 に答える 1

1

このエラーが発生する理由は、行数をカウントしているときにスキャナーオブジェクトmyScanners.get(i - 1)がファイルの終わりに到達するためです。したがって、もう一度読み込もうとすると、それはあなたが残した場所、つまりファイルの終わりから始まります。

さまざまな方法がありますが、最も簡単なのは新しいスキャナーオブジェクトを作成することだと思います。

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

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

        lineCounts.add(lineCount);
    }

 myScanners.clear()

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

 // Find the minimum file count.
int minLineCount = Collections.min(lineCounts);
于 2012-11-17T00:50:52.457 に答える