My scanner class isn't reading the next line from the .txt file
I'm trying to read a text file that looks like this
0,-16,-4,12,10,4,-14,8,44,8,8,12,-4
1,-16,-4,12,10,4,-14,6,43,10,10,12,-4
2,-16,-6,10,11,2,-13,4,43,10,11,12,-4
3,-16,-6,10,11,2,-13,6,43,10,11,11,-4
4,-16,-6,10,11,2,-13,6,42,8,10,10,-4
However I am only able to the first line then it stops, I'm using开发者_如何学运维 the hasNextInt function in a for loop and it reaches the end of the first line and then hasNextInt = null. What method can I use to carry on reading into the next line?
Thanks in advance for any help you can give me.
Here's the code:
public final static int numChannels = 12; // the data is stored in 12 channels, one for each lead
public final static int numSamples = 500*6; //500 = fs so *6 for 6 seconds of data
public File file;
private Scanner scanner;
short [] [] ecg = new short [numChannels] [numSamples];
public ECGFilereader (String fname) throws FileNotFoundException
{
File file = new File(Environment.getExternalStorageDirectory() +"/ecg.txt");
scanner = new Scanner(file);
scanner.useDelimiter(",");
}
public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 3000 samples)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++)
{
String x = scanner.next();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++)
{
ecg [chan] [m] = (short) scanner.nextInt();
}
}
for (int chan=0; chan<numChannels; chan++)
waves[chan].setSignal(ecg[chan]); // sets a signal equal to the ecg array of channels
return true;
}
}
I would check for the case when hasNextInt()
returns false, and add the following:
if (!scanner.hasNextInt())
{
if (scanner.hasNextLine())
{
scanner.nextLine();
}
}
This will consume the rest of the line and set the scanner to be at the beginning of the next line.
精彩评论