简体   繁体   中英

Writing a file in Java

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.

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());
    }
}

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM