Results 1 to 3 of 3
  1. #1
    Paperkut is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default sorting read data from a file into text area by highest score

    Hi, well my project consist of GUI of simple textfield where user enters there name and then a simple game where he/she gets score once you finnish the game. i wrote the name and score onto a file and then read it into text area. The problem is now that i need to sort that data by highest score for overall top 10 scores. i do no use database, its just simple reading/wrting from file into text area.

    Here is the reading/writing methods, i'm not sure where to begin in sorting the scores...

    Java Code:
    public void writeFile() {
    		try {
    			FileWriter write = new FileWriter("text.txt", true);
    			PrintWriter text = new PrintWriter(write);
    			text.println( name + " " + score);
    			text.flush();
    			write.close();
    
    		} catch (IOException ioe)
    		// writes name from textfield(username) and score from board class
    		// into text document text.txt
    		{
    			ioe.printStackTrace();
    		}
    	}


    Java Code:
    try {
    			FileReader fileStream = new FileReader("text.txt");
    			area.read(fileStream, "text.txt");
    			fileStream.close();
    			
    		} catch (FileNotFoundException e) 
    		// gets data from text.txt and reads
    		// it into textarea called area
    		{
    			System.out.println("File not found");
    		} catch (IOException e)
    				{
    			System.out.println("IOException occurred");
    		}

  2. #2
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    Try This I think this is what you are looking for
    sorting - Optimal way to sort a txt file in Java - Stack Overflow

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    1) Create a "BestScore" class. This class will have two properties, "name" and "score". You will then need to implement the Comparable method for the class the sort on score.

    2) You can't read the text file directly into the text area you will need to read the file line by line

    3) for each line in the file:
    a) parse the data to get the name and score
    b) create a BestScore object using the name and score
    c) add the BestScore object to an ArrayList

    4) Sort the ArrayList
    5) loop through the items of the ArrayList and add them to your text area.

    Here is an example of sorting with hard coded data. You will need to fill in the rest to get your data from the file.

    Java Code:
    /*
    **  Use the Collections API to sort a List for you.
    **
    **  When your class has a "natural" sort order you can implement
    **  the Comparable interface.
    **
    **  You can use an alternate sort order when you implement
    **  a Comparator for your class.
    */
    import java.util.*;
    
    public class Person implements Comparable<Person>
    {
    	String name;
    	int age;
    
    	public Person(String name, int age)
    	{
    	   	this.name = name;
    	   	this.age = age;
    	}
    
    	public String getName()
    	{
    		return name;
    	}
    
    	public int getAge()
    	{
    		return age;
    	}
    
    	public String toString()
    	{
    		return name + " : " + age;
    	}
    
    	/*
    	**  Implement the natural order for this class
    	*/
    	public int compareTo(Person p)
    	{
    		return getName().compareTo(p.getName());
    	}
    
    	static class AgeComparator implements Comparator<Person>
    	{
    		public int compare(Person p1, Person p2)
    		{
    			int age1 = p1.getAge();
    			int age2 = p2.getAge();
    
    			if (age1 == age2)
    				return 0;
    			else if (age1 > age2)
    				return 1;
    			else
    				return -1;
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		List<Person> people = new ArrayList<Person>();
    		people.add( new Person("Homer", 38) );
    		people.add( new Person("Marge", 35) );
    		people.add( new Person("Bart", 15) );
    		people.add( new Person("Lisa", 13) );
    
    		// Sort by natural order
    
    		Collections.sort(people);
    		System.out.println("Sort by Natural order");
    		System.out.println("\t" + people);
    
    		// Sort by reverse natural order
    
    		Collections.sort(people, Collections.reverseOrder());
    		System.out.println("Sort by reverse natural order");
    		System.out.println("\t" + people);
    
    		//  Use a Comparator to sort by age
    
    //		Collections.sort(people, new Person.AgeComparator());
    		Collections.sort(people, new AgeComparator());
    //		Collections.sort(people, new PersonAgeComparator());
    		System.out.println("Sort using Age Comparator");
    		System.out.println("\t" + people);
    
    		//  Use a Comparator to sort by descending age
    
    		Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
    //		Collections.sort(people, Collections.reverseOrder(new PersonAgeComparator()));
    		System.out.println("Sort using Reverse Age Comparator");
    		System.out.println("\t" + people);
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  2. Class employee,company,test: Read data from text file.
    By jeskoston in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 01:50 AM
  3. Replies: 4
    Last Post: 02-15-2011, 11:05 PM
  4. Replies: 0
    Last Post: 11-21-2010, 05:10 PM
  5. Replies: 40
    Last Post: 07-04-2008, 08:41 AM

Posting Permissions

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