gpt4 book ai didi

java - "Line not available"读取 nextLine() 时出错;从文件扫描仪

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:15 28 4
gpt4 key购买 nike

我正在尝试编写一个程序来读取多个文件并逐行比较它们。要读取的文件数量取自用户,文件的格式预计为“input1.txt”、“input2.txt”等。

当我尝试运行代码时,出现“NoSuchElementException”,告诉我 Scanner.nextLine() line: line not available。该代码位于第 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();
}
}

}

最佳答案

您收到此错误的原因是在计算行数时扫描仪对象 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);

关于java - "Line not available"读取 nextLine() 时出错;从文件扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426479/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com