Errors while reading files

Asked

Viewed 21 times

1

I made a method to read the txt file that was created in another method. After long receiving the Numberformatexception, I managed to eliminate it but the method still does not send name and time to my class Playorranking. After a few tests I see that lerRanking is not correctly storing the contents of the two variables, I just don’t know why.

This method writes txt:

try {
        if (!arquivo.exists()) {
            arquivo.createNewFile();
        }
        FileWriter fw = new FileWriter(arquivo, true);
        BufferedWriter escrever = new BufferedWriter(fw);
        escrever.write(nome);
        escrever.newLine();
        escrever.write(tempo);
        escrever.newLine();
        escrever.close();
        fw.close();
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

}

And this method would read the file and return the name and time:

public static JogadorRanking lerRanking(String arquivo) throws IOException {
    fileR = new FileReader(arquivo);
    buffR = new BufferedReader(fileR);
    JogadorRanking ranking = new JogadorRanking("", 0);
    String linha = buffR.readLine();
    while (linha != null) {
        String nome = linha;
        linha = buffR.readLine();
        int tempo = Integer.parseInt(linha);
        ranking.adicionaJogador(nome, tempo);
        linha = buffR.readLine();
    }
    Collections.sort(ranking.getDadosRanking());
    return ranking;
}

Here the class Jogadorranking (a little summed up, obvious), which I use to sort the ranking:

public JogadorRanking(String nome, int tempo) {
    this.nome = nome;
    this.tempo = tempo;
}
public void adicionaJogador(String nome, int tempo) {
    dadosRanking.add(new JogadorRanking(nome, tempo));
}
public String retornaJogador() {
    String dados ="";
    for(JogadorRanking jogador: dadosRanking) {
        dados+=jogador.retornaNome()+" "+jogador.retornaTempo()+"\n";
    }
    return dados;
}

Although there are no mistakes, when I call on main to print the return Player()[that returns the names and times of the players in the list], nothing appears. If I try to make a . lerRanking("..."). getName and getTempo, the name comes out empty and time 0. Could someone please tell me where I’m going wrong?

  • arrives and enter the while? what the value of linha?

  • Welcome to Stack Overflow! Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.

  • It enters while, putting to print the line value after the first buffR.readline(), I get "line equal to 3". In the last readline()[after int time = Integer.parseint and ranking.adder], I get line equal to null

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.