String Split, Column Grouping & Calculate Average

Question

I am able to print out the entire text file, but I want to print student name, total number of quizzes taken and average of scores. This is 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.

 

Answer

First, split each line into the string of letters and the string of numbers; split the string of numbers into numbers by whitespaces; then calculate the average. It’s difficult to do this in Java. It’s rather easy to do this in SPL (Structured Process Language):

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: Import source.txt as a single-field sequence of strings.

undefined

A2: Split each string member in A1’s sequence into a sequence by white spaces and parse members into appropriate data types.

undefined

A3: Divide each member of A2’s sequence into two groups - a non-numeric data group and a numeric data group.

undefined

A4: Create a two-dimensional table where column 1 is student name, column 2 is score and column 3 is average.

undefined

An SPL script is integration-friendly. See How to Call an SPL Script in Java to learn more.