Hi, I'm trying to read a .txt with 10 million numbers, and it only reads 5 million and I really don't know where the problem is. Here is the code of the method I am using:
public double promedioDouble() throws IOException
{
double acumulador = 0;
int contador = 0;
try{
file = new FileReader(ruta);
reader = new BufferedReader(file);
while(reader.readLine()!=null)
{
acumulador+=Double.parseDouble(reader.readLine());
contador++;
}
}
catch (IOException e)
{
System.out.println("cant read");
}
finally {
if (file != null) file.close();
}
return acumulador/contador;
}
I did a print to the counter and it only counts 5 million.
Note that in the loop you are doing twice
reader.readLine()
, so you are eating the lines 2 by 2 instead of 1 by 1, that is why the file has just been read with half the number of iterations that you expected and only performs the sum of the even rows. You would have to do something like this to fix it:So you only read one line per iteration.