This site requires JavaScript, please enable it in your browser!
Greenfoot back
JetLennit
JetLennit wrote ...

2013/9/2

Reading/Editing .txts

JetLennit JetLennit

2013/9/2

#
I want to make a high score saver in java with a text file, the file is called highscore. I wondered how to save an int if that int is larger than the one that is already in the file. Could someone please help?
danpost danpost

2013/9/3

#
You mean without the use of the UserInfo class?
JetLennit JetLennit

2013/9/3

#
Precicly
danpost danpost

2013/9/3

#
Key 'read write file' in the search box and check out some of the related discussions.
davmac davmac

2013/9/3

#
As danpost says a search is a good idea. But if you still need help, please make your questions more specific. A general answer to your question is: 1. Read the existing file and get the high score from it 2. Compare it to the current score 3. If the current score is higher, write it back to the file.
JetLennit JetLennit

2013/9/3

#
What I would like is a basic thing that is this word-code
if(highscore.txt.getText().toInt() < score)
{
     delete(highscore.txt, inJar());
     writeWithText(highscores.txt, score.toString(), inJar());
}
How would i do that?
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
What part are you having problems with? The saving and loading of files or the interpretation of the data and checking whether the highscore values are higher that the current once?
JetLennit JetLennit

2013/9/3

#
Yes, I have never worked with this sort of thing and would like help with both.
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
Ok to read and write files you should use a suport class like my File Reader/Writer Demo. Using the methods from this project you can easily save and load files. The interpretation of the data is the more difficult part. To know who made a specific high score you need to save the name of the player with the score he/she reached. Then you can compare the name of the player in the file with the name of the player that has done the new highscroe (using equals to compare the strings). Then you have the score of this player as a string because in the txt file you can't realy store integers. To make the text an int you can use the method Integer.parseInt(String) which returns an int value of the string. Now you can compare the score of the player with his/her current score and maybe change the value. To save the new highscore you should load all the highscores (best would be to save them in a java.util.List object because this object is not static like arrays) and save all highscores again. Therefore you should not save the strings in the List but create a new class that stores the name of the player and the score he/she reached. Then you can also sort the list of values you have in your highscore table.
davmac davmac

2013/9/3

#
Hmm, by this:
     delete(highscore.txt, inJar());  
     writeWithText(highscores.txt, score.toString(), inJar());  
You mean you want to write to a file that's within your jar file? That's not really possible.
JetLennit JetLennit

2013/9/3

#
davmac wrote...
Hmm, by this:
     delete(highscore.txt, inJar());  
     writeWithText(highscores.txt, score.toString(), inJar());  
You mean you want to write to a file that's within your jar file? That's not really possible.
What about the file that the jar is in?
davmac davmac

2013/9/3

#
I'm not sure what you mean by that. The "jar" is a file (it's a "jar file"). It's not normally within another file. In any case, you can't normally write to it.
JetLennit JetLennit

2013/9/3

#
For example, lets say that the jar is in "C:\JarLocation\Jar.jar\" how would I save it to "C:\JarLocation\"?
Gevater_Tod4711 Gevater_Tod4711

2013/9/3

#
If you want to save a text fiel from a jar file the text file is saved in the same folder where the jar file is located. But you can't realy write into the jar file.
davmac davmac

2013/9/3

#
For example, lets say that the jar is in "C:\JarLocation\Jar.jar\" how would I save it to "C:\JarLocation\"?
Ah, I see what you mean. I think the first problem you have to solve is writing to a file generally. To get the location of the jar file is tricky. In BlueJ (and Greenfoot) we use code something like: Boot.class.getResource("Boot.class").toString() ... to get a kind of URL which has the location of the jar file (you can use any class name, it doesn't have to be Boot, but it must be inside the jar). The result looks something like this: jar:file:/C:/home/bluej/bluej/lib/bluej.jar!/bluej/Boot.class So you see it does contain the path to the jar file, but you need to extract it.
You need to login to post a reply.