Good day!
I have a project (game) that needs to be presented tomorrow morning. But I discovered a bug when writing in the high scores. I am trying to create a text file and write the SCORE NAME in descending order using score as the basis.
FOR EXAMPLE:
SCORE NAME RANK
230 God
111 Galaxian
10 Gorilla
5 Monkey
5 Monkey
5 Monkey
NOTE THERE'S ALSO A RANK My code is as follows:
public void addHighScore() throws IOException{
boolean inserted=false;
File fScores=new File("highscores.txt");
fScores.createNewFile();
BufferedReader brScores=new BufferedReader(new FileReader(fScores));
ArrayList vScores=new ArrayList();
String sScores=brScores.readLine();
while (sScores!=null){
if (Integer.parseInt(sScores.substring(0, 2).trim()) < score && inserted==false){
vScores.add(score+"\t"+player+"\t"+rank);
inserted=true;
}
vScores.add(sScores);
sScores=brScores.readLine();
}
if (inserted==false){
vScores.add(score+"\t"+player+"\t"+rank);
inserted=true;
}
brScores.close();
BufferedWriter bwScores=new BufferedWriter(new FileWriter(fScores));
for (int i=0; i<vScores.size(); i++){
bwScores.write((String)vScores.get(i), 0, ((String)vScores.get(i)).length());
bwScores.newLine();
}
bwScores.flush();
bwScores.close();
}
But if i input three numbers: 60 Manny, the file would be like this:
60 Manny
230 God
111 Galaxian
10 Gorilla
5 Monkey
5 Monkey
5 Monkey
The problem is it can only read 2 numbers because I use sScores.substring(0, 2).trim()).
I tried changing it to sScores.substring(0, 3).trim()). but becomes an error because it read upto the character part. Can anyone help me in revising my code so that I can read upto 4 numbers? Your help will be highly appreciated.