I wrote a simple console application to show you how to read from and write to text files in Java. Basically it generates two HTML files (exam results based on a data file) in two formats. There is a version for students (results sorted by student id) and a version for a teacher (sorted by result). For Java programming I recommend IntelliJ IDEA.
A data file looks like this. First name then surname, student system id and number of points achieved. An example data file:
Chandler Bing 987622345 10 Joey Tribbiani 983455234 1 Phoebe Buffay 345678434 21 Rachel Green 345634456 4
We want to store these lines in an array of objects so there is a class with variables needed, getters and setters and some more methods I will describe later. What’s important I use comparators (needed for sorting). I need data sorted by two keys so I use two comparators.
/** * Created by Krzysztof Grabowski on 04.03.15. */ import java.util.Comparator; public class Student { //public class Student implements Comparable<Student> { // Needed to use: @Override public int compareTo() private String studentname; private String studentsurname; private int systemid; private int points; public Student(String studentname, String studentsurname, int systemid, int points) { this.studentname = studentname; this.studentsurname = studentsurname; this.systemid = systemid; this.points = points; } public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this.studentname = studentname; } public String getStudentsurname() { return studentsurname; } public void setStudentsurname(String studentsurname) { this.studentsurname = studentsurname; } public int getSystemid() { return systemid; } public void setSystemid(int systemid) { this.systemid = systemid; } public int getPoints() { return points; } public void setPoints(int points) { this.points = points; } /*@Override public int compareTo(Student comparesto) { int comparepoints=((Student)comparesto).getPoints(); *//* For Ascending order*//* //return this.points-comparepoints; *//* For Descending order do like this *//* return comparepoints-this.points; }*/ // Comparator for sorting the list by systemid public static Comparator<Student> StuSystemid = new Comparator<Student>() { public int compare(Student s1, Student s2) { int systemid1 = s1.getSystemid(); int systemid2 = s2.getSystemid(); /*For ascending order*/ //return systemid1-systemid2; /*For descending order*/ return systemid2-systemid1; } }; // Comparator for sorting the list by points public static Comparator<Student> StuPoints = new Comparator<Student>() { public int compare(Student s1, Student s2) { int points1 = s1.getPoints(); int points2 = s2.getPoints(); /*For ascending order*/ //return points1-points2; /*For descending order*/ return points2-points1; } }; @Override public String toString() { return studentname + " " + studentsurname + " " + systemid + " " + points; } }
As you can see there is a few lines commented. If you need only one way of sorting you can uncomment them and comment those two comparators instead and use the following code somewhere in your StudentApp to do the sorting.
Continue reading Introduction to Java: input, output, file operations