creating a highscore with file io in Java -
i have been attempting create highscore file.io have not been able it. code have.
public void highscore () { int highscore = 0; string line = ""; try { bufferedreader reader = new bufferedreader (new filereader ("highscores")); while ((line = reader.readline ()) != null) // read score file line line { try { int score = integer.parseint (line); // parse each line int if (score > highscore) // , keep track of largest { highscore = score; } } catch (numberformatexception e1) { // ignore invalid scores //system.err.println("ignoring invalid score: " + line); } line = reader.readline (); } reader.close (); } catch (ioexception ex) { system.err.println ("error reading scores file"); } c.println (highscore); }
my text file has numbers in in each line
i guess get:
"error reading scores file"
you should fix filename extension:
bufferedreader reader = new bufferedreader (new filereader ("highscores.txt"));
and make sure file lies in project's folder.
also, notice read 2 lines file each time, @ end of while loop , in while header, delete line = reader.readline ();
@ end of loop.
Comments
Post a Comment