1

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.

3 Answers 3

3

What you should use is:

String[] parts = sScrores.trim().split("\\s+", 2);

Then you will have an array with the number at index 0, and the name in index 1.

int theNumber = Integer.parseInt(parts[0].trim();
String theName = parts[1].trim();

You could re-write the while-loop like so:

String sScores=brScores.readLine().trim();
while (sScores!=null){
        String[] parts = sScrores.trim().split(" +");
        int theNumber = Integer.parseInt(parts[0].trim();
        if (theNumber < score &&     inserted==false){
                vScores.add(score+"\t"+player+"\t"+rank);
                inserted=true;
        }
        vScores.add(sScores);
        sScores=brScores.readLine();
}

Personally, I would add a new HighScore class to aid in parsing the file.

class HighScore {

    public final int score;
    public final String name;
    private HighScore(int scoreP, int nameP) {
        score = scoreP;
        name = nameP;
    }

    public String toString() {
        return score + "  " + name;
    }

    public static HighScore fromLine(String line) {
        String[] parts = line.split(" +");
        return new HighScore(Integer.parseInt(parts[0].trim()), parts[1].trim());
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Was writing the same thing :P
where would i put this code? String theName = parts[1].trim();
@newbie, you don't need that in your code I don't believe. (from what I can see)
@jjnguy where would i String theName = parts[1].trim(); code so that it would not affect the format of my file?
@jjinguy THANK YOU VERY MUCH... you are a life saver.. I'll read the link you gave me. Thank you again!
|
2

The format of each line is always the same : an integer, followed by a tab, followed by the player name.

Just find the index of the the tab character in each line, and substring from 0 (inclusive) to this index (exclusive), before parsing the score.

The player name can be obtained by taking the substring from the tab index +1 (inclusive) up the the length of the line (exclusive).

Comments

1

if above mentioned table is a file.

for first two score it will be fine but for 5 it start reading character. Might be that is causing problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.