在Java中从多个txt文件读取数据

在Java中从多个txt文件读取数据,java,file,input,Java,File,Input,我是一名计算机科学入门的初学者,我正在使用java。在getLetterGrade方法中尝试读取多个txt文件时,此程序中出现以下错误: 线程“main”java.lang.IllegalStateException中的异常:扫描程序已关闭 import java.util.Scanner; import java.io.*; public class Exam { private int grade; private float examAvg; private St

我是一名计算机科学入门的初学者,我正在使用java。在getLetterGrade方法中尝试读取多个txt文件时,此程序中出现以下错误:

线程“main”java.lang.IllegalStateException中的异常:扫描程序已关闭

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

public class Exam
{
    private int grade;
    private float examAvg;
    private String name;
    private File exam1Data;
    private File exam2Data;
    private File namesData;
    private Scanner inFileExam1;
    private Scanner inFileExam2;
    private Scanner inFileName;

    //constructor
    public Exam() throws IOException
    {
        exam1Data = new File("exam1.txt");
        exam2Data = new File("exam2.txt");
        namesData = new File("names.txt");
        inFileExam1 = new Scanner(exam1Data);
        inFileExam2 = new Scanner(exam2Data);
        inFileName = new Scanner(namesData);
    }

public float getExam1Avg()
{
examAvg = 0;

while (inFileExam1.hasNext()) {
grade = inFileExam1.nextInt();
examAvg += grade;
}
inFileExam1.close();
examAvg = (float)(examAvg / 25.0);
return examAvg;
}

public float getExam2Avg()
{
examAvg = 0;

while (inFileExam2.hasNext()) {
grade = inFileExam2.nextInt();
examAvg += grade;
}
inFileExam2.close();
examAvg = (float)(examAvg / 25.0);
return examAvg;
}

    //method that finds the letter grade given a student name and
    //the exam number
    public char getLetterGrade(String inputName, int examNum)
    {
        char letter;
        int count = 1;

        //inputName = inputName.toLowerCase();

        do {
            if (inFileName.hasNext()){
                name = inFileName.nextLine();
            }
            count++;
        } while (inFileName.hasNext() && !name.equalsIgnoreCase(inputName));

        inFileName.close();

        if (!name.equalsIgnoreCase(inputName)) {
            return 'x';
        }

        if (examNum == 1) {
            for (int i = 1; i <= count && inFileExam1.hasNextInt(); i++) {
                grade = inFileExam1.nextInt();
            }
            inFileExam1.close();
        }
        else if (examNum == 2) {
            for (int i = 1; i <= count && inFileExam2.hasNextInt(); i++) {
                grade = inFileExam2.nextInt();
            }
            inFileExam2.close();
        }
        else {
            return 'x';
        }

        if (grade >= 90) {
            letter = 'A';
        }
        else if (grade < 90 && grade >= 80) {
            letter = 'B';
        }
        else if (grade < 80 && grade >= 70) {
            letter = 'C';
        }
        else if (grade < 70 && grade >= 60) {
            letter = 'D';
        }
        else {
            letter = 'F';
        }

        return letter;
    }
}

您正在关闭扫描仪

inFileName.close();
然后在不打开的情况下再次尝试使用它,如下所示:

inFileName = new Scanner(..); 
EDIT1 这对我来说并没有失败,但也许我错过了一些东西(我仍然不知道你到底有多愿意阅读这些文件,它们之间的关系如何)

public char getLetterGrade(字符串inputName,int examNum){
字符字母;
整数计数=0;
做{
if(inFileName.hasNext()){
name=inFileName.nextLine();
}
计数++;
}while(inFileName.hasNext()&&!name.equalsIgnoreCase(inputName));
如果(!name.equalsIgnoreCase(inputName)){
返回'x';
}
如果(examNum==1){
对于(int i=1;i=80){
字母='B';
}否则,如果(等级<80&&grade>=70){
字母='C';
}否则,如果(等级<70和等级>=60){
字母='D';
}否则{
字母='F';
}
回信;
}

首先,每次读一行时,你都在重置
等级
,而你什么也没做,因此部分代码是无用的

但要回答你的问题:

根据API,
Scanner
仅在使用
Scanner.close()
关闭对象或系统以某种方式关闭对象后,才会返回
IllegalStateException

尝试将
扫描仪
infieexam1
声明移动到您使用它的位置旁边,如下所示:

if (examNum == 1) {
        inFileExam1 = new Scanner(exam1Data); // Add this
        for (int i = 1; i <= count && inFileExam1.hasNextInt(); i++) {
            grade = inFileExam1.nextInt();
        }
        inFileExam1.close();
    }
    else if (examNum == 2) {
        inFileExam2 = new Scanner(exam2Data); // And this
        for (int i = 1; i <= count && inFileExam2.hasNextInt(); i++) {
            grade = inFileExam2.nextInt();
        }
        inFileExam2.close();
    }
}
public void close() {
    inFileName.close();
    inFileExam1.close();
    inFileExam2.close();
}
if(examNum==1){
inFileExam1=新扫描仪(exam1Data);//添加此

对于(int i=1;i您没有包含包含主方法的代码,但我将有根据地猜测您在其中构造了一个新的
example
对象,并多次调用
getletletgrade
。因为您在构造函数中打开了
扫描仪,阅读后关闭它们,所以第二次调用
getLetterGrade
您将看到一个
非法状态异常

如果这种心理解读不准确,你能用
main(String[]args)
的内容更新你的帖子吗


编辑:在您最近的编辑之后,我看到您在调用
getLetterGrade
之前调用了
getExam1Avg
方法。因为
getExam1Avg
扫描仪上调用了
close
,所以在调用另一个方法时,您会得到一个非法状态异常。您需要为每个方法调用实例化一个新的扫描仪,或者否则,请在每个方法完成后重置扫描仪。

您的错误出现在
getAverageExam1Avg
和另一个方法中。您不应在
getLetterGrade
方法中关闭
扫描仪,而应使其保持打开状态,并添加如下
close
方法:

if (examNum == 1) {
        inFileExam1 = new Scanner(exam1Data); // Add this
        for (int i = 1; i <= count && inFileExam1.hasNextInt(); i++) {
            grade = inFileExam1.nextInt();
        }
        inFileExam1.close();
    }
    else if (examNum == 2) {
        inFileExam2 = new Scanner(exam2Data); // And this
        for (int i = 1; i <= count && inFileExam2.hasNextInt(); i++) {
            grade = inFileExam2.nextInt();
        }
        inFileExam2.close();
    }
}
public void close() {
    inFileName.close();
    inFileExam1.close();
    inFileExam2.close();
}
确保将适当的异常添加到该代码中

现在失败的原因是您尚未打开扫描仪。请在构造函数中取消对实例化的注释


如果这不起作用,我不知道会发生什么。

两个
infieexam*
文件中的数据是什么?exam1.txt数据:67 56 90 77 45 87 23 12 87 89 96 44 55 66 22 88 99 77 59 100 2 47 60 8 81 exam2.txt数据:76 34 11 77 44 33 77 97 79 71 72 45 54 68 32 83 49 88 79 81 93 58 75所有数字都写在单独的行上。。我不知道这是否重要。老兄,你只是让你的文章变得更复杂和不可读。坚持主要问题。你能解释一下你的代码应该如何处理所有这些文件吗?这不是扫描仪的问题。这是inFileExam1扫描仪的问题。那么提供所有代码,我们就会看到它。否则,这个例外选项只是因为多个扫描程序实例在没有关闭的情况下被实例化。我还有其他方法,如果您只是删除/注释掉“inFileName.close();”,我会将它们添加到您的代码中。所有操作都会正常工作,因为我们不确定您存储的文件中有什么,所以我们无法调试它(我无法),但只要我现在尝试读取文件,获取输出,它就可以工作,只需注释/删除这行。嗯..我删除了它,但它仍然不起作用。我现在必须离开。我回家后会回来检查。感谢所有人迄今为止的帮助,我很感激,希望你能继续帮助我!!我添加了与你一样的代码,但现在我得到了以下运行时错误:线程“main”java.lang.RuntimeException中的异常:不可编译的源代码-未报告的异常java.io.FileNotFoundException;必须捕获或声明为引发…:(使方法引发一个
IOException
如下:
getLetterGrade(…)引发IOException{
我注释掉了构造函数中的inFileExam1和inFileExam2赋值,还为getLetterGrade方法添加了“throws IOException”…但是现在我得到了一个不同的错误:线程“main”java.lang.NullPointerException中的异常…它说错误首先发生在以下行:while(inFileExam1.hasNext()){你的意思是
while(inFileName.hasNext(){
?否则,我在代码中看不到那一行。是否有其他类正在运行此操作?如果是,请编辑添加了该代码的问题。我只是将其更改为while(inFileName.hasNextInt()){但我得到了同样的错误..我用主类和我的全部代码更新了原始帖子。我不知道如何编辑主帖子…但我只是制作了一个考试对象并使用System.out.println(getLetterGrade(“name”,1));@user1082587在你问题的底部,左边有一个
编辑
按钮。谢谢大家的帮助。我拿走了构造函数、文件和扫描字段声明。我创建了文件和扫描
public void close() {
    inFileName.close();
    inFileExam1.close();
    inFileExam2.close();
}