Help with FileReader

0

I have a query that is happening something strange in my code, I put the code and I continue to explain

public void leerPuntuaciones(){     
        try {
            String puntuacion1 = "";
            BufferedReader bf = new BufferedReader (new FileReader("nivel 1.csv"));
            String linea = bf.readLine();
            while((linea=bf.readLine())!=null){
                //System.out.println(linea);

                String partes[] = linea.split(",");
                puntuacion1 = partes[0] +" "+ partes[1];
            }

            puntuaciones1.add(puntuacion1);
        } catch (IOException e) {
            e.printStackTrace();
        }

Now, in that file that I am reading, I only recognize the last line and what I want to do in that puntuación1 is to add each one of the lines,

Do I have to add the bf.readline in a for or something like that?

    
asked by osmodiar16 04.05.2017 в 06:35
source

1 answer

0

The error is logical you read all the lines but the add to the list you do when you finish reading, so just add the last one since when you leave the while the value of puntuacion1 will be the last line read

To solve, just move the add within the while{...} so that each line read by readLine is added to the list

while((linea=bf.readLine())!=null){
      String partes[] = linea.split(",");
      puntuacion1 = partes[0] +" "+ partes[1];
      puntuaciones1.add(puntuacion1);
 }
    
answered by 04.05.2017 / 06:54
source