problem is to read a file containing marks for some modules, add the marks to find the total and then print out the records in descending order based on the total mark.
So far i've coded enough to see what's in the file, i now need to work out how to sort it.
Im thinking i create a class Student, and then when each line is read pass the info in as instance variables for a Student object. Then perhaps try and insert each object into an ArrayList and sort.
code for the reading and displaying bit is below, just after any hints on direction to follow.
any help appreciated :)
public class Results
public static void main(String args)throws IOException
String name = null;
String init = null;
int math = 0;
int phys = 0;
int chem = 0;
int total = math + phys + chem;
boolean endFile = false;
FileInputStream marksFile = new FileInputStream("marks");
DataInputStream marksStream = new DataInputStream(marksFile);
while(endFile == false)
name = marksStream.readUTF();
init = marksStream.readUTF();
math = marksStream.readInt();
phys = marksStream.readInt();
chem = marksStream.readInt();
total = math + phys + chem;
System.out.println(" " + name + " " + init + " " + math + " " +
phys + " " + chem + " " +total);
endFile = true;
System.out.println("Error file not found");
System.out.println("Problem reading file");
Sounds like a plan to me. Check out Collections.sort(). The API is your friend.
Yes, put the all values in an appropriate collection and sort it.