Salvatore Amore
Salvatore Amore

Reputation: 5

[JAVA]How to create a Ranking System then saving it to a .txt file?

So, I have a "Memory Game", you can input your name, choose the difficulty(4x4 or a 6x6 game) and then start the game. When you click Start, a new Panel will pop up and the game will start.

The buttons will be randomized and for each mistake you make, you lose 2 points and for every right combination, you gain 10 points. At the end or if you click on the Exit button, a message will pop up stating the Player's Name, how many tries he did(clicked 2 different buttons) and how many Points he has. Then the game ends and it doesn't save the Player's Score.

Now, my problem is, I don't know how to implement a Ranking System in my code. It would be something basic, like, a comparison between all the Scores and rearrange them to the one with the most points comes first and so on.

So from what I researched, I would need a Save method that whenever someone finishes a game it would save their scores in a .txt file and an Array method that would arrange the scores form Best to Worst.

Here's the whole code;

http://pastebin.com/6Wtiju7z

private void mostrarResumoJogo() { 
    resumoJogo = "Jogador: " + objJogadorJogada.getNome() + " " +
            "Pontos: " + objJogadorJogada.getPontos() + " " +
            "Quantidade de tentativas: " + qtdeTentativas;

    JOptionPane.showMessageDialog( null, "" + resumoJogo, "Resumo do Jogo",
            JOptionPane.INFORMATION_MESSAGE ); 

    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter( new FileWriter("Ranking.txt") );
        writer.write(resumoJogo);
    }
    catch ( IOException e) { }
    finally {
        try {
            if (writer != null)
                writer.close( );
        }
        catch ( IOException e) { }
    }

    setVisible( false );
}

The problem is that the file is always overwritten with a new .txt I already tried to create a type File attribute so that he doesn't always create another .txt but with no success.

It's the last thing that I need to do on this code, but I can't seem to figure it out, please, help.

Upvotes: 0

Views: 1074

Answers (3)

Pshemo
Pshemo

Reputation: 124225

The problem is that the file is always overwritten with a new .txt

Problem is here

writer = new BufferedWriter( new FileWriter( "Ranking.txt"));

Each time you invoke new FileWriter( "Ranking.txt") it creates new empty file. If you want to add more data to already existing file you need to open it in append mode via

writer = new BufferedWriter( new FileWriter( "Ranking.txt", true));
//                                           add this part -^^^^ 

Upvotes: 1

Sarel Botha
Sarel Botha

Reputation: 12700

To make it not overwrite, but append instead pass append=true when calling the FileWriter constructor.

Instead of doing that though I would recommend just reading and writing the whole file every time. This is because the scores have to be sorted and rewritten to the file.

I would recommend using JAXB to create an XML file. Here is a tutorial for using JAXB to do this: http://www.mkyong.com/java/jaxb-hello-world-example/

Upvotes: 0

Mikhail Matvienko
Mikhail Matvienko

Reputation: 216

Probably this is what are you looking for

https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)

Just specify APPEND parameter as true and your changes won't be overwritten

Upvotes: 0

Related Questions