Java 使用循环将文本文件打印到终端

Java 使用循环将文本文件打印到终端,java,while-loop,Java,While Loop,我可以打印出整个文本文件,但我希望它能打印出学生姓名、参加的测验总数和平均分数。 这是输出: Student Name Number of Quizzes Average Kirk James Tiberius 95 86 87 92 95 93 91 94 Summers Buffy 76 57 98 92 91 83 85 Baker Tom 100 100 100 100 100 100 100 100 100 100 100 100 100 100 1

我可以打印出整个文本文件,但我希望它能打印出学生姓名、参加的测验总数和平均分数。 这是输出:

Student Name         Number of Quizzes           Average
Kirk James Tiberius 95 86 87 92 95 93 91 94
Summers Buffy 76 57 98 92 91 83 85
Baker Tom 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Reynolds Malcolm 82 81 83 85 84 86 88 76 93
Bennet Elizabeth 91 93 95 94 89 93 95 96 94
Blutarsky John 0 0 0 0 0 0 0 0 0
Gale Dorthy 86 75 91 88 88 87 
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at studenttester.StudentTester.main(StudentTester.java:36)
Java Result: 1
以及输出:

Student Name         Number of Quizzes           Average
Kirk James Tiberius 95 86 87 92 95 93 91 94
Summers Buffy 76 57 98 92 91 83 85
Baker Tom 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Reynolds Malcolm 82 81 83 85 84 86 88 76 93
Bennet Elizabeth 91 93 95 94 89 93 95 96 94
Blutarsky John 0 0 0 0 0 0 0 0 0
Gale Dorthy 86 75 91 88 88 87 
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at studenttester.StudentTester.main(StudentTester.java:36)
Java Result: 1
这是我的代码:

package studenttester;

public class Student {
    private String name;
    private double totalScore;
    private int numQuizzes;
    String grade;

    /**
     * Returns the name of a student
     *
     * @return the name of the student
     */
    public String getName() {
        return name;
    }

    /**
     * Adds a quiz score to the total quiz score of the student
     *
     * @param score the score of a quiz
     */
    void addQuiz(int score) {
        numQuizzes += 1;
        totalScore += score;
    }

    /**
     * Returns the total score of all the quizzes combined that student took
     *
     * @return the value of score
     */
    double getTotalScore() {
        return totalScore;
    }

    /**
     * Returns the average score of all the quizzes a student took
     *
     * @return
     */
    double getAverageScore() {
        return totalScore / numQuizzes;
    }
}


我想我需要循环来识别整数和名称,但不确定如何识别。

您是否有一个文件数据的示例,它会生成您所遇到的错误?是否有代码将“goodData”设置为false?否则,它将是无限循环,扫描器很快将无法读取任何内容。@Tom谢谢,格式看起来好多了。与@MadProgrammer的想法相同。我认为您的输入数据的一小部分样本将对社区有所帮助,因为可能有一种更简单的方法来做到这一点。例如,如果它是一个CSV文件,那么我们可能可以使用一些库来干净地完成工作。它只是一个没有记录的文件,上面写着:Kirk James Tiberius 95 86 87 92 95 93 91 94 Summers Buffy 76 57 98 92 91 83 85 Baker Tom 100 100 100 100 100 100 100 100 100 Reynolds Malcolm 82 81 83 85 84 86 88 76 93班纳特·伊丽莎白91 93 95 94 89 93 95 96 94布卢塔斯基·约翰0 0 0 0 0 0 0 0盖尔·多尔西86 75 91 88 87-10