Results 1 to 3 of 3

Thread: Sorting Problem

  1. #1
    Moorag is offline Member
    Join Date
    Mar 2011
    Location
    Cardiff
    Posts
    3
    Rep Power
    0

    Default 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");
    		}
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default

    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!

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Sorting Problem
    By Freakzoyd in forum New To Java
    Replies: 5
    Last Post: 11-30-2010, 05:40 AM
  2. sorting problem
    By vasug in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 04:55 AM
  3. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  4. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  5. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •