Multi-line Text File Import and Sort

Question

My project has names and scores for 16 students in a class. A text file has their names and their lab/quiz scores. I need to read this data into parallel arrays. This is what the text file looks like:

 

Line 1: Puckett, Karen

Line 2: 10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0

Line 3: 4 3 5 3 5 2 3 2 1.5 1 5 3.5

Line 4: 17.5 24 22 23.5 22 23

Line 5: 90 91

Line 6: 96

 

This is repeated for each student with no gap lines. The format in the file is:

·         Line 1: name

·         Line 2: lab grades

·         Line 3: quiz grades

·         Line 4: project grades

·         Line 5: exam grades, and

·         Line 6: final exam grade.

This format is repeated for each of the sixteen students.

I'm struggling with how to read them into each line into their own arrays. Would I need to make one for each student? In the end I must be able to sort the students by grade, but I'm not asking for help with that. My problem is how I would read the data of each student and their grades into arrays; and how to store them so that I would be able to distinguish which grades belong to which students. Below is what I have done so far:

 

public class JavaApplication39 {

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) throws Exception {

        // TODO code application logic here

        //declare file object, and connect to scanner  object

        File myFile = new File(System.getProperty("user.dir") + "/src/scores.txt");

        if (!myFile.exists()) {

            System.out.println("Unable to open the file");

            System.exit(0);

        }

        Scanner inputFile = new Scanner(myFile);

 

        //create method to return percent earned

        //create name array

        String[] nameList = getNameList(inputFile);

        System.out.println(Arrays.toString(nameList));

 

        //create array of grades and then get average and return array of averages

        double[] labGrade = getLabGrade(inputFile);

        System.out.println(Arrays.toString(labGrade));

    }

 

    public static String[] getNameList(Scanner object) {

        String[] nameList = new String[16];

        while (object.hasNext()) {

            for (int i = 0; i < 16; i++) {

                nameList[i] = object.nextLine();

                object.nextLine();

                object.nextLine();

                object.nextLine();

                object.nextLine();

                object.nextLine();

            }

        }

        return nameList;

    }

 

    public static double[] getLabGrade(Scanner object) {

        double[] labGrade = new double[15];

        double[] labGradeSum = new double[15];

        int count = 0;

        object.nextLine();

        for (int i = 0; i < 15; i++) {

            labGrade[i] = object.nextDouble();

        }

        object.nextLine();

        object.nextLine();

        object.nextLine();

        object.nextLine();

        object.nextLine();

        object.nextLine();

    return labGrade ;

    }

}

 

Answer

According to your description, every 6 lines consist of one record and a sort of records is required. Since final_exam_grade contains single values, it can be easily sorted. lab grades stores sequences, averages need to be calculated before a sort. It’s hard to code these structured computations in Java. You can handle them in SPL (Structured Process Language) and then embed the SPL script into a Java application. See How to Call an SPL Script in Java.

A

1

=file("d:\\data.txt").import@is()

2

=A1.(substr(~,":"))

3

=A2.group((#-1)\6).new(~(1):name,   ~(2).split@t(" "):lab_grades, ~(6):final_exam_grade)

4

=A3.sort(final_exam_grade)

5

=A3.sort(-lab_grades.avg())

A1: Import text file data.txt;

 undefined

A2: Extract strings after the colon;

 undefined

A3: Group data every 6 lines, and create a table sequence of 3 fields - name, lab_grades, and final_exam_grade, in which lab_grades values are sequences;

  undefined

A4: Sort records by final_exam_grade in ascending order;

A5: Then sort records by lab_grades in descending order.