Skip to main content
gamedev.net
🔒 Locked

[java] Java FIleWriter help

Started by lldanll 2 replies 2.5k views
Original Post
lldanll
lldanll
k..I made a simple space invaders type game and i decided to put a high score display in the game.. When the game is over it will check the old high score with your score and then overwrite this score with yours if you beat it. I decided to use a simple text file to hold the score and then read in the score and then simply overwrite the file with the new score... however i have not really done anything with reading and writing in such a long time that i can not remember what exactly to do.. I can read in the highscore from the file, however i have to: int highScore = Integer.parseInt(file.readLine()); in order to get the score to not be in the string format that it is read in as.. then i have another method that checks the original highscore with the new score and if it is greater then i have: FileWriter file = new FileWriter(new File("highScores.txt"); file.write(score); file.close; This works as far as writing to the file, but it is converting the integer score into string value( ex. if the score is 65, itll write A) I can not for the life of me remember how to write the integer value to the text file... can someone help me out here. thanks alot!
Elehisie
Elehisie
String scoreToWrite = String.parseStr(score);
and then you write scoreToWrite into file instead?


im not sure if thats the write way to put that line ok? and im too tired now to go look for you. but you NEED to convert it to string yourself before writing, to make sure its a string showing '65' instead of 'A'

hope it helps
lldanll
lldanll
alright Thanks! I got it to work finally... I had to do what you said but instead of:

String highScore = String.parseStr(score)

you need:

String highScore = String.valueOf(score);

I was tring to convert the int to string and write it before but i could never figure out what String method to use..so i just took a guess with the valueOf(int) method and it seems to work fine... Thanks again for the help!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.