1

i have a bufferedreader like this:

BufferedReader br = BufferedReader(new FileReader(("Output/pointsSorted.txt")));

    String line;
    while((line = br.readLine()) != null){
        System.out.println(br.readLine());
    }
    br.close();
}

but it only reads every second line and i dont understand why.

This is the file where the reader reads from:

bendsan: 1000
gotr_gotey: 87
lordelain: 87
nightbot: 87
vellsain: 87
r3l10n: 86
skalrayi: 85
brom13: 84
llecrezzef: 84
cloudinger: 74
littleangelswing: 60
n3belmacht: 43
mrscandy_: 32
sluckzz: 31
elboy717: 30
assassinenfan: 18
msurai: 18
woischdesdu: 16
daspaddy: 14
sirfullmetal: 10
daranun: 1

and this is the output:

gotr_gotey: 87
nightbot: 87
r3l10n: 86
brom13: 84
cloudinger: 74
n3belmacht: 43
sluckzz: 31
assassinenfan: 18
woischdesdu: 16
sirfullmetal: 10   

as you can see every second line is skipped.

Skalrayi
  • 13
  • 4
  • 2
    Every `readLine` consumes one line, including call in `while` condition so you are doing it twice in each iteration but printing only result from second call. – Pshemo Nov 28 '16 at 18:30

3 Answers3

0

Because you're printing br.readLine() instead of line, therefore reading 2 lines per each iteration of the loop (but printing only one).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0
BufferedReader br = BufferedReader(new FileReader(("Output/pointsSorted.txt")));

    String line;
    while((line = br.readLine()) != null){ // line 1
        System.out.println(br.readLine()); // line 2
    }
    br.close();
}

Line 1 reads a line from the file and stores it in line. Then line 2 reads a new line from the file and outputs it. So the first line is read into line, then the second line is printed out, then the third line is read into line, then the fourth line is printed out, and so on.

You probably wanted this:

BufferedReader br = BufferedReader(new FileReader(("Output/pointsSorted.txt")));

    String line;
    while((line = br.readLine()) != null){
        System.out.println(line);
    }
    br.close();
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

You are calling readline() twice - once inside the while's condition (and then ignoring the result) and once inside its body. Just remove one of the calls, and you should be OK. E.g.:

String line;
while ((line = br.readLine()) != null) {
    System.out.println(line); // Use the value of line populated in the while's condition
}
br.close();
Mureinik
  • 297,002
  • 52
  • 306
  • 350