Sort existing data with new data (external txt-file)
OK. First of all, this will be my first post on this forum, I just found it and I'm pretty happy about it.
Short story of why I'm posting: I study information science at a University in Sweden and recently started a course in Java-programming(OOP).
We recieved an assignment bout a week ago and I've been trying to finish this project all week. Every time I start trying my brain freezes and I just dont know how to fix this problem.
The problem: We're making a highscore-list that sends strings to a external .txt-file. While doing so I need to sort the current data (in the textfile) with the new highscore I'm adding. Also, I need to erase anything that's not in the top 5 on the highscorelist.
Not sure if I've made myself clear, my english is not perfect so please ask if you dont understand what I need help with.
What I'm asking for: I really need some sort of guidance to finish this project, so help me please!
(I seem to have problems with indenting the code on the forums, that's why I've added pastie-links to all classes)
Just click the bold text.
Quote tags don't use preformatted text elements. Using the [php][/php] or [code][/code] will retain the formatting. For which yours is very good.
So let me see if I understand this correctly. You only need to keep in the file the top five entries is that correct? If I add another entry that is < any of the top 5, it is just abandoned, and if I insert an entry > any of the top 5 than it shifts the lowest out and inserts the new one?
Are you allowed to use in-memory variable allocations for the class? The easiest way to do this involves an array or collection, for which you can sort and makes it easy to tell if an item can fit in it. A *slight* pain when it comes to shifting an array such as removing the lowest score and inserting a new one in the middle which pushes everything else down, but that can be worked around by simply overwriting the lowest and then sorting it again. If you're familiar with collections, a LinkedList is the one for this.
Operation wise for an application this small I'd load from a file during an initialization command. Keep what I can in memory for usage, then on quit I'd save it back in.
Also- I was reading that class (PrintWriter) documention and from my understanding it will always truncate the file. Also it will create the file if it does not exists (ie you can comment out your new File statement since you are not doing anything special with the File object before passing it to the printwriter)
What I mean is that you will always be overwriting it so if you only write one player/score then there would only be 1 entry- so I would suggest in memory collection as Fou-Lu stated. But I could have misread that Class document as well :)
Thanks for your reply.
Yes Fou, that's exactly what I need. I think I will use an array for it. I'm sitting with it right now trying to fix things up. What I first did was creating a new class called Player. In the player-class I will create an array to both place and sort the highscore list.
Also I've been adding a method for reading the file, saving to memory so it can re-apply the data before it starts sorting/erasing all players that's >5.
I will let you know how that worked out for me later today :)
Let us know if you have any issues we can help you with.
|All times are GMT +1. The time now is 05:34 AM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.