Ove Lindström wrote:Do you need to store the highscore list in a readable text form or just as persistence between runs?
If the first, then you are on the right track. If the second, why not just serialize the whole array/list to a file and read it when you start the program?
AnujS Sharma wrote:Why are you unable to set the "HighScore"??
Are you getting a compiler error or is it not working as expected??
If it is the first one, then please post the error for us to help you. If it is the latter one, for what all inputs are you not getting the expected output.
My advice would be to identify such inputs and debug through your code until you find the root cause of the problem. Change the code appropriately and test again.
Paul Clapham wrote:What other reply?
AnujS Sharma wrote:The exception thrown says that it cannot find the file.
When you're specifying the file, specify the absolute path. Maybe the the file that you want to read & write is not in the same directory or sub directory as that of the your java class file. This is why it is throwing an exception. Try specifying the whole path like, "D:\FolderName\HighScore.txt".
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:don't apologize for asking questions...there is a LOT to learn, and we all have to start somewhere.
Assuming your file is in the D:\Downloads directory, and assuming it really is named "Highscore.txt", then yes...almost.
remember that in String literals in java, a backslash character '\' has special meaning. so if you do it exactly as you wrote, java will think the "\D" and "\H" are 'special characters', and will most likely complain. you need to tell it you want a LITERAL backslash, and the way you do that is to use TWO in a row:
(new FileReader("D:\\Downloads\\Highscore.txt"));
at least...i think. I haven't tested this...but this is the basic idea.
Note that it will also vary depending on what OS you are using...windows is different from Linux is different from MacOS. Somone (probably Campbell) will most likely chime in soon and tell you the RIGHT way to do it with directory separators, but this should work for now.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:also, and this is a WAG...does the file actually exist?
based on the code in your original post, the first thing you try and do is open it with the reader. I'm not sure what that does if the file isn't already there, but a "Filenotfound" exception sounds like a reasonable thing to happen.
if it is not, try putting a file there, even if it is empty.
AnujS Sharma wrote:Did you change it for the FileReader as well??
Try debugging the program. Use System.out.println() to see till where the program is executing and at what line precisely you are getting the error.
I can see that you haven't pasted the full code, may be that's why we are unable to help you..
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Try this...
Write a brand new program, something like "FileReadTest.java". Have it do nothing but open the file, read and print each line, then close the file.
See if you can get that to work. If not, post THAT code here, since it should only be about 20-30 lines, max.
If it DOES work, then look at what may be different between your new test program and your existing one. Running from the same directory? File name the same? etc.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |