Results 1 to 3 of 3
Thread: Sorting Problem
- 03-11-2011, 08:21 PM #1
Member
- Join Date
- Mar 2011
- Location
- Cardiff
- Posts
- 3
- Rep Power
- 0
Sorting Problem
Hello guys
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 :)
Java Code:import java.io.*; import java.util.*; 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; try { FileInputStream marksFile = new FileInputStream("marks"); DataInputStream marksStream = new DataInputStream(marksFile); while(endFile == false) { try { 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); } catch(EOFException e) { endFile = true; } } marksStream.close(); } catch(FileNotFoundException e) { System.out.println("Error file not found"); } catch(IOException e) { System.out.println("Problem reading file"); } } }
- 03-11-2011, 08:27 PM #2
Sounds like a plan to me. Check out Collections.sort(). The API is your friend.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-12-2011, 12:58 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, put the all values in an appropriate collection and sort it.
Similar Threads
-
Sorting Problem
By Freakzoyd in forum New To JavaReplies: 5Last Post: 11-30-2010, 05:40 AM -
sorting problem
By vasug in forum Advanced JavaReplies: 2Last Post: 02-25-2010, 04:55 AM -
Problem: Arrays and Sorting
By Rhez in forum New To JavaReplies: 7Last Post: 02-03-2010, 02:18 PM -
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 08:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks