Reputation:
In the game that I am currently working on I want to make it so that when you start the game you get the previous score you had befor you quit. I have already made a save file for the score with this code.
try{
File getScore = new File("Score.dat");
FileOutputStream scoreFile = new FileOutputStream(getScore);
byte[] saveScore = score.getBytes();
scoreFile.write(saveScore);
}catch(FileNotFoundException ex){
}catch(IOException ex){
}
The score is displayed as a String so therefor upon start the game have to get the score inside the .dat file as an string so that I can equal the score string to the string that was generated upon start. I have tried using the code shown below.
try{
BufferedReader br = new BufferedReader(new FileReader("Score.dat"));
score = br.toString();
}catch (FileNotFoundException ex){
}
But when I use that code I get this error message.
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "java.io.BufferedReader@313159f1"
Upvotes: 0
Views: 87
Reputation: 93842
If you do br.toString()
, it calls the toString()
method from the class object on your BufferedReader object. So it prints the memory adress of your buffered object :
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
That's why you get a NumberFormatException
because you can't assign a String
to score
(which I suppose is an int
variable).
Furthermore, you are absolutely not looking for that because you want the text stored in your file. If you want to read a single line from your buffer you just have to do this :
String line = br.readLine();
int value = Integer.parseInt(line);
Upvotes: 1