am trying to read a file (double values) with scanner and save the data into an array. until this point it working great. the strange that am trying to do is that i want to read certain values(the first 10) from the file, add them on the array, do something with those values and then read the next 10 values and continue doing it until the eof.
until now am only able to read once the first 10 values and can not read the second group of values.
the code that am using is:
File fi= new File(Environment.getExternalStorageDirectory().toString()+"/ECG/I.txt");
scanner = new Scanner(fi);
scanner.useDelimiter(";");
int SAMPLE_SIZE=4;
double timi[]= new double[SAMPLE_SIZE];
for (int size=0; size<SAMPLE_SIZE; size++)
{
while(scanner.hasNext()&&size<SAMPLE_SIZE) {
timi[size]=scanner.nextDouble();
Log.i("File","String " + size +" "+timi[size]);
size++;
}
Assuming your “read certain values (the first 10)” is your
SAMPLE_SIZE(currently 4), you could do something like this:This does not currently account for situations where the number of entries in the file is not a multiple of
SAMPLE_SIZE. This could easily be extended to account for this situation, but is an exercise left to the reader – at least for now.