hey guys, this is my first time trying java i/o on greenfoot
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!
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)));} }