简体   繁体   中英

Why does BufferedWriter refuse to write a file?

I am making a snake game for my class project in school. Everything was fine until I looked at my code. It produced no errors when I "build" it but returns NumberFormatException.

PS I am only a 1st year college student. Please be gentle

    private void checkScore() throws IOException{
        try{
    bw = new BufferedWriter(new FileWriter("scores.txt"));
    if(score > Integer.parseInt(highest)){
                highest = Integer.toString(score);
            } else {
            }
                }catch(IOException e){
    }
    bw.write(score);

    bw.close();
    }

The result I want from this is that when a player ends the game, the score would get recorded. If its a high score then it would be recorded in a scores.txt file. But my BufferedWriter refuses to write a file which results to the error mentioned.

The problem is that highest has not a valid integer value when you do Integer.parseInt(highest) . Use debug options to see which value contains and fix that problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM