简体   繁体   中英

Reading from a txt file, sorting, and outputing to another txt file

I hope someone will take the time to help me. I am new to Java and am taking a class to try and learn it. I have an assignment that I have started and deleted probably 30 times. This one just isn't clicking with me. The assignment is as follows:

Read 3 columns of integers from a txt file. One column has a student number, one column a score for an assignment and the 3rd column is for the maximum possible score for that assignment. (There are 5 students with 10 scores each).

I must use at least 1 array.

List the total points received from the assignments and total the maximum points possible for each of the 5 students for the 10 assignments. Then output the student number, scores, maximum possible score for the assignment, percent grade and finally the student letter grade to a different txt file.

The txt file looks like this:

12345 10 15
12345 25 25
12345 89 100
12345 23 25
12345 9 10
12345 42 50
12345 75 100
12345 92 100
12345 40 50
12345 48 50
23456 12 15
23456 23 25
23456 99 100
23456 24 25
23456 9 10
23456 47 50
23456 88 100
23456 95 100
23456 35 50
23456 45 50
34567 10 15
34567 24 25
34567 87 100
34567 23 25
34567 9 10
34567 45 50
34567 97 100
34567 98 100
34567 38 50
34567 48 50
45678 8 15
45678 21 25
45678 78 100
45678 22 25
45678 9 10
45678 46 50
45678 76 100
45678 92 100
45678 37 50
45678 39 50
56789 8 15
56789 21 25
56789 78 100
56789 21 25
56789 8 10
56789 42 50
56789 72 100
56789 88 100
56789 32 50
56789 34 50

This is what I have so far:

import java.awt.List;    
import java.io.FileNotFoundException;    
import java.io.FileReader;    
import java.util.ArrayList;    
import java.util.Arrays;    
import java.util.Scanner;    
import java.util.IOException;

public class gradesSummary {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Integer> l1 = new ArrayList<Integer>(1);
        ArrayList<Integer> l2 = new ArrayList<Integer>(2);
        ArrayList<Integer> l3 = new ArrayList<Integer>(3);


        Scanner s = new Scanner(new FileReader("grades.txt")); 

        while (s.hasNext()) {
            l1.add(s.nextInt());
            l2.add(s.nextInt());
            l3.add(s.nextInt());
        }

        s.close();

        System.out.println(l1);  
        System.out.println(l2);  
        System.out.println(l3);
    }   
}

Where do I go from here?

I have completed most of my code. I now need to print out to a new text file. Here's my new code:

public class GradesSummary { public static void main(String[] args) throws FileNotFoundException {

     int x = 1;
        Scanner inputFile = new Scanner(new File("grades.txt"));
        int[] studentNum = new int[100];
        int[] score = new int[100];
        int[] maxScore = new int[100];
        int[] totalScore = new int[6];
        int[] totalMaxScore = new int[6];
        int[] studentNumber = new int[6];
        double[] percentage = new double[6];
        //int[] totalScore = new int[100];
        //int[] totalMaxScore = new int[100];

        //int totalMaxScore = 0;
        //int totalScore = 0;
        //double percentage = 0.00;
        studentNum[0] = 0;
        while(inputFile.hasNext()){
                studentNum[x] = inputFile.nextInt();
                score[x] = inputFile.nextInt();
                maxScore[x] = inputFile.nextInt();
                x++;            
        }
        int y = 0;
        studentNumber[y] = studentNum[1]; 
        for (x = 1; x < 100; x++){
            if (x > 1 && studentNum[x] != studentNum[x-1]){
                percentage[y] = (double)totalScore[y] / (double)totalMaxScore[y];
                y++;
                studentNumber[y] = studentNum[x];
            }

            totalMaxScore[y] += maxScore[x];
            totalScore[y] += score[x];
        }

        for (int z = 0; z<= 4; z++){
            System.out.println(z + " " +studentNumber[z]+" "+percentage[z]);
        }
      }

}
//inputFile.close();

Any suggestions for creating a new text file to print the studentNum, totalscore, totalmaxscore, percentage grade and letter grade?

Thanks for any help you can give.

The OOP way to do this is create a Student class, with int id and an array of scores for assignments. Note that each student receives the same assignments in the same order, so you can create an array of the 10 maximum scores and store the sum.

do like this

your data

    ArrayList<Integer[]> list = new ArrayList<Integer[]>();
    Scanner s = new Scanner(new FileReader("grades.txt")); 
    while (s.hasNext()) {
        Integer[] array= new Integer[3];
        array[0]=s.nextInt();
        array[1]=s.nextInt();
        array[2]=s.nextInt();
        list.add(array);
    }
    s.close();

Your Comparator

class SimpleComparator implements Comparator<Integer[]> {    
    @Override
    public int compare(Integer[] o1, Integer[] o2) {
        if(!o1[0].equals(o2[0])){
            return o1[0].compareTo(o2[0]);
        }
        if(!o1[1].equals(o2[1])){
            return o1[1].compareTo(o2[1]);
        }
        if(!o1[2].equals(o2[2])){
            return o1[2].compareTo(o2[2]);
        }
        return 0;
    }        
}

your sorting

  Collections.sort(list,new SimpleComparator());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM