This site requires JavaScript, please enable it in your browser!
Greenfoot back
mrbrook
mrbrook wrote ...

2019/3/25

NumberFormatException on reading files

mrbrook mrbrook

2019/3/25

#
hey guys, this is my first time trying java i/o on greenfoot
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.*;

public class Score extends Actor 
{   
List <Integer> scorelist = new ArrayList<Integer>();

public void setscore(int currentscore)
    {
        this.currentscore = currentscore;
    }

public int getscore()
    {
        return currentscore;
    }

private void readfile()
    {
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader("highscore.txt"));
            String r = br.readLine();
            while(r != null)
            {   
                {scorelist.add(Integer.parseInt(r));}
                r = br.readLine();
                
            }           
        } catch (IOException ex) {System.out.println(ex);}
    }

    private void writefile()
    {   
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("highscore.txt",true));
            bw.newLine();
            bw.write(String.valueOf(getscore()));
            System.out.println("writed");
        }
        catch (IOException ew) {System.out.println(ew);}
        finally {   
           try {
               if(bw != null){
            bw.close();
             }
        } catch (IOException e) {
        e.printStackTrace();
          }
        }

    }

    public String formatscore(int a){
        return (String.format("%02d : %02d", a/3600,(a/60)%60));
    }

    public void savescore()
    { 
        writefile();
        scorelist.clear();
        readfile();
        countstart = false;
        Collections.sort(scorelist, Collections.reverseOrder()); 
        if(scorelist.size() == 6)
        {  
            scorelist.remove(5);
            
        }
    }

    public void showhighscore()
    {   
        scorelist.clear();
        readfile();
        Collections.sort(scorelist, Collections.reverseOrder());
        for(int i =0; i<scorelist.size(); i++)
{System.out.println(formatscore(scorelist.get(i)));}
    }
and it always catch the NumberFormatException like this, java.lang.NumberFormatException: For input string: "3859" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at Score.readfile(Score.java:62) at Score.showhighscore(Score.java:114) at MyWorld.act(MyWorld.java:202) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) any idea to get the read file added to this scorelist with integer type? and is it necessary to close my bufferedreader? thanks for being helpful!
mrbrook mrbrook

2019/3/25

#
I just noticed there's space on( java.lang.NumberFormatException: For input string: " 3859") part, while there was no space on my text file it seems like the main problem is on my readfile() method. but I have no idea what's wrong with my code
danpost danpost

2019/3/25

#
I would open the text file in a text editor (Notepad would do) and make sure that there is no white space contained in the file. Remove any blank lines and spaces and save the file.
mrbrook mrbrook

2019/3/25

#
I've done multiple checks on my text files, sir, it seems no space before the first sentence nor each line
danpost danpost

2019/3/25

#
mrbrook wrote...
I've done multiple checks on my text files, sir, it seems no space before the first sentence nor each line
It must be something with your text file as I have tested the code and I am not getting any errors. Oh, I did, however add a:
br.close();
-- inserted between lines 33 and 34.
mrbrook mrbrook

2019/3/25

#
I just build a new file to replace, it might be an unknown error with that space before the very first line, anyway this br.close help my code get better execution :) thanks for your respond
You need to login to post a reply.