从Java中的多个文本文件中读取数据

时间:2011-12-06 00:55:34

标签: 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 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;
    }
}

错误发生在以下行:

for (int i = 1; i <= count && inFileExam1.hasNextInt(); i++) {

请帮助!!

这是主要课程:

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

public class ExamAverages
{
public static void main(String[] args) throws IOException
{
Exam exam = new Exam();
System.out.println(exam.getExam1Avg());
System.out.println(exam.getExam2Avg());
System.out.println(exam.getLetterGrade("name", 1));
}

4 个答案:

答案 0 :(得分:0)

您正在关闭扫描仪

inFileName.close();

然后尝试再次使用它而不打开如下:

inFileName = new Scanner(..); 

<强> EDIT1 这对我来说并没有失败,但也许我错过了一些东西(我仍然不知道你是多么愿意阅读这些文件,它们之间的关系如何)

public char getLetterGrade(String inputName, int examNum) {
    char letter;
    int count = 0;

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

    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;
}

答案 1 :(得分:0)

首先,每次阅读一行时你都会重置grade,并且你没有对它进行任何操作,因此部分代码是无用的。

但要回答你的问题:

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

尝试将您Scanner的{​​{1}}声明移到您正在使用它的旁边,如下所示:

inFileExam1

答案 2 :(得分:0)

您不包含具有主要方法的代码,但我会做出有根据的猜测,即在其中构建新的Exam对象,并多次调用getLetterGrade 。由于您在构造函数中打开Scanners并在阅读后关闭它们,因此在您致电getLetterGrade时,您会看到IllegalStateException

如果此精神阅读不准确,您可以使用main(String[] args)的内容更新您的帖子吗?

编辑:在您进行最新修改后,我发现您在调用getExam1Avg之前调用了getLetterGrade方法。由于getExam1Avgclose上调用Scanner,因此在调用其他方法时会出现IllegalStateException。您需要为每个方法调用实例化一个新的扫描程序,或者在每个方法完成后重置扫描程序。

答案 3 :(得分:0)

您的错误在getAverageExam1Avg和另一个错误。不要在Scanner方法中关闭getLetterGrade,而应该将它们保持打开并添加close方法,如下所示:

public void close() {
    inFileName.close();
    inFileExam1.close();
    inFileExam2.close();
}

确保在该代码中添加适当的例外。

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

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