Jaehyuck Choi
Jaehyuck Choi

Reputation: 179

How to sort the average mark with elements in a text file in Java

There is a text file which include student names, scores. I got the average marks of each student by using a Constructor. But, I have no idea how to sort the average marks with elements in the text file.

These are the element in the text file.

    public class Student {
        String name;
        int physic;
        int chemistry;
        int math;
        int ave;

        public Student(String name, int physic, int chemistry, int math) {
            this.name = name;
            this.physic = physic;
            this.chemistry = chemistry;
            this.math = math;

            int sum = this.physic + this.chemistry + this.math;
            this.ave = sum / 3;
        }

        public void setName(String name) {
            this.name = name;
        }
        public void setPhysic(int physic) {
                this.physic = physic;
        }
        public void setChemistry(int chemistry) {
            this.chemistry = chemistry;
        }
        public void setMath(int math) {
            this.math = math;
        }

        public String getName() {
            return this.name;
        }
        public int getPhysic() {
            return this.physic;
        }
        public int getChemistry() {
            return this.chemistry;
        }
        public int getMath() {
            return this.math;
        }

        public int getAve() {
            return this.ave;
        }

    }
    import java.io.*;
    import java.util.Comparator;

    class Main {
        public static void main(String[] args) {
                Student[] student = readData("students.txt");
            sortStudents(student);
            display();

        }
    private static Student[] readData(String filename) {
            int nStudent = 0;
            Student[] student = new Student[20];
            //filename = "students.txt";
            String string = null;

            try {
                FileReader fileReader = new FileReader(filename);
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                while((string = bufferedReader.readLine()) != null) {
                        String[] arr = string.split(" ");
                        student[nStudent] = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));

                    nStudent++;
                }
                bufferedReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return student;
        }

        private static void display() {
            Student[] students = readData("students.txt");
            sortStudents(students);
            System.out.println("Student     AverageScore      Physic        Chemistry         Maths");
            for (int i = 0; i < students.length; i++) {
                System.out.println(students[i].getName() + "\t\t" + students[i].getAve() + "\t\t" + students[i].getPhysic() + "\t\t" + students[i].getChemistry() + "\t\t" + students[i].getMath());
            }
        }
    public static Student[] sortStudents(Student[] student) {
        //I need to sort the average marks of each student in the descending order.
        }
    }

Upvotes: 1

Views: 829

Answers (2)

Jon Thoms
Jon Thoms

Reputation: 10767

You just need to change your smaller methods.

main():

public static void main(String[] args) {
    display();
}

display():

private static void display() {
    Student[] students = readData("students.txt");
    System.out.println("Student     AverageScore      Physic        Chemistry         Maths");
    for (Student student : students) {
        int[] sortedGrades = sortStudentGrades(student);
        System.out.println(student.getName() + "\t\t" + sortedGrades[0] + "\t\t" + sortedGrades[1] + "\t\t" + sortedGrades[2] + "\t\t" + sortedGrades[3]);
    }
}

The last thing that I changed was to change the sortStudents() method to the sortStudentGrades() method. Sorting students doesn't really do much good, and you there's no field to store the grades in the current Student object. As such, you need something that can sort an array of grades with the average without changing the Student object.

sortStudents() -> sortStudentGrades():

public static int[] sortStudentGrades(Student student) {
    int[] grades = new int[] {student.getAve(), student.getChemistry(), student.getPhysic(), student.getMath()};
    Arrays.sort(grades);
    return grades;
}

Hope that helps!

Upvotes: 1

Moleson
Moleson

Reputation: 169

First take care about double cast. But you can implement the Comparable class like this

class Student implements Comparable<Student> {
private String name;
int physic;
int chemistry;
int math;
double ave;

public Student(String name, int physic, int chemistry, int math) {
    this.name = name;
    this.physic = physic;
    this.chemistry = chemistry;
    this.math = math;

    int sum = this.physic + this.chemistry + this.math;
    this.ave = (double)sum / 3;
}

@Override
public int compareTo(Student o) {
    return Double.compare(o.ave, ave);//for descending order
}    
}

and in your main you can use Arrays.sort(Student_tab); e.g:

import java.util.Arrays;

public static void main(String[] args) {
    Student a = new Student("john", 40, 23, 44);
    System.out.println("a = "+ a.ave);
    Student b = new Student("john", 50, 23, 44);
    System.out.println("b = "+ b.ave);

    Student[] tab = {a,b};
    Arrays.sort(tab);
    System.out.println(" 1: "+ tab[0].ave);
    System.out.println(" 2: "+ tab[1].ave);
}

Indeed the array of students is ordered we get the following result:

//Disorder:

a = 35.666666666666664
b = 39.0

//Ordered:

1: 39.0
2: 35.666666666666664

Upvotes: 0

Related Questions