0

I'm new to Java programming and I want to make a mini snake game. So far, I've worked on the basics of the snake's function and everything except the leaderboards which I want to have in my game to keep track of the highscores. I watched few of programming videos on making a snake game and I have tried adding ", true" in the writeFile to record the present highscore but I can only record 2 scores, the previous highscore and current highscore. After that I get an error if I reached the 3rd highscore in the game. How do I append or how can I modify this code?

//this is for the score in the user interface
public String getHighScore() {
    FileReader readFile = null;
    BufferedReader reader = null;
    try {
        readFile = new FileReader("highscore.txt");
        reader = new BufferedReader(readFile);
        return reader.readLine();       
        } catch (Exception e) {
        return "Nobody:0";
        }
    finally {
        try {
            if(reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//this is the method i created to record the highscore in game to text file
public void checkScore() {
    if(hs.equals(""))
        return;
    if(appleEaten > Integer.parseInt((hs.split(":")[1]))) {
        String name = JOptionPane.showInputDialog("You have set a new highscore. What is your name?");
        hs = name + ":" + appleEaten;
        
        File scoreFile = new File("highscore.txt");
        if (!scoreFile.exists()) 
        {
            try {
                scoreFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter writeFile = null;
        BufferedWriter writer = null;
        try {
            writeFile = new FileWriter(scoreFile);
            writer = new BufferedWriter(writeFile);
            writer.write(this.hs);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

When I'm going for the 3rd highscore, this is the error that I get

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2Stephen"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at GamePanel.checkScore(GamePanel.java:253)
at GamePanel.checkCollisions(GamePanel.java:154)
at GamePanel.actionPerformed(GamePanel.java:177)
at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:310)
at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:242)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

0 Answers0