拆串后再列间分组计平均

【问题】

I am able to print out the entire text file, but i want it to print the student name, total number of quizzes taken, and the average of the scores. This is the output:

And the output:

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

This is my Code:

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

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class StudentTester {
    public static void main(String[] args) throws IOException {
        int digit = 0, amount = 0, quizzes = 0, sum = 0, next = 0;
        String name;
        boolean goodData = true;

        System.out.println("Student Name         Number of Quizzes           Average");

        Scanner reader = new Scanner(new File("quizScores.txt"));

        while (goodData) {
            name = reader.next();
            System.out.print(name);
            System.out.println(reader.nextLine());

            if (reader.hasNextInt()) {
                digit = reader.nextInt();
                sum = sum + digit;
                quizzes++;
            }
            sum = next;
            quizzes = next;
        }
        double quiz = 0;
        while (goodData) {
            name = reader.next();
            System.out.print(name);
            System.out.println(reader.nextLine());

            if (reader.hasNextInt()) {
                digit = reader.nextInt();
                sum = digit + sum;
                quizzes++;
                sum = next;
                quizzes = next;
            }
            System.out.println(name);
            System.out.println(sum / quizzes);
        }
    }
}

It’s just a file out of note pad that says : 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 -10

【回答】

先将每行分为字母串和数字串两部分,再将数字串拆成多个数字,最后计算平均值。Java写起来太麻烦,建议用SPL做,简单许多:


A

1

=file("d:\\source.txt").import@si()

2

=A1.(~.split@p(" "))

3

=A2.(~.group(ifnumber(~)))

4

=A3.new(~(1).concat(""),~(2).concat("   "),~(2).avg())

 

A1:读取source.txt文件内容,读成单字段串构成的序列。

undefined

A2:将A1序列中的字符串成员按照分隔符 拆成序列,并将成员解析成对应的数据类型。

A3:对上一步结果进行分组,将非数值型数据分一组,数值型数据分一组。

A4:生成二维表,第一列为名字,第二列为成绩,第三列为平均成绩。

这段代码也很容易集成进JAVA参考Java 如何调用 SPL 脚本