1

好的,所以我正在编写一个 Java 应用程序来导入一个 csv 文件,然后循环遍历结果,并将它们加载到一个数组中。我正在正确导入文件,因为它没有通过异常。我的问题是,当我尝试计算 FileInputStream 中的记录数时,我陷入了无限循环。这里可能是什么问题。继承人的代码:

这是我的类,它有一个调用 go() 的 Main 方法:

public void go() {
    pop = new PopularNames();
    popGui = new PopularNamesGui();
    String file = popGui.userInput("Enter the correct name of a file:");
    pop.setInputStream(file);
    pop.getNumberOfNames();
}

这是类 PopularNames (pop),在下面的方法中,我将 inputStream var 设置为新的 FileINputStream。文件名由用户提供。

public void setInputStream(String aInputStream) {
    try {
        inputStream = new Scanner(new FileInputStream(aInputStream));
    } catch (FileNotFoundException e) {
        System.out.println("The file was not found.");
        System.exit(0);
    }
}

这是麻烦的方法。我只是循环通过 FileInputStream 并计算记录数:

public void getNumberOfNames() {
    while (this.inputStream.hasNext()) {
        fileDataRows++;
    }
}
4

1 回答 1

7
public void getNumberOfNames() {   
  while (this.inputStream.hasNext()) {   
     inputStream.nextLine(); // Need to read it so that we can go to next line if any
     fileDataRows++;    
  } 
}
于 2011-10-18T05:24:32.620 回答