Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting data from a file into textarea

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting data from a file into textarea

    Hi, i'm coding this simple childrens learning game and to summarise it consists of textfield where user inputs there name and then he/she gets to play this game where at the end you recieve a score. Using read/write methods i made the name and the score to appear on file and then it displays it on textarea. The problem is now i need to sort the scores in the file and display top 10 in the text area. I'm not sure how or where to begin

    here is the read/write methods:
    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();
    		}
    	}


    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)
    		// this just basic highscores with limited
    		// capabilities
    		{
    			System.out.println("IOException occurred");
    		}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Sorting data from a file into textarea

    I would create an Object that contains one user record- a name and a score. Create a List of these record Objects. Then use a custom Comparator that compares the scores along with the Collections.sort() method to sort the List. Display the first 10 items in the List, and you'll be displaying the top 10 scores (assuming you wrote the Comparator to sort them from high to low).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 8
    Last Post: March 25th, 2011, 03:34 PM
  2. Using File Data
    By computercoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2010, 08:51 PM
  3. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 10:16 PM
  4. sorting data in excell
    By jaya in forum Member Introductions
    Replies: 5
    Last Post: September 18th, 2010, 08:11 PM
  5. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM