我的扫描仪类没有读取 .txt 文件中的下一行
我正在尝试读取一个如下所示的文本文件
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
,但是我只能读到第一行,然后它就停止了,我在 for 循环中使用 hasNextInt 函数,它到达第一行的末尾,然后 hasNextInt =无效的。我可以用什么方法继续读到下一行?
预先感谢您能给我的任何帮助。
这是代码:
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'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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将检查当
hasNextInt()
返回 false 时的情况,并添加以下内容:这将消耗该行的其余部分并将扫描仪设置为下一行的开头。
I would check for the case when
hasNextInt()
returns false, and add the following:This will consume the rest of the line and set the scanner to be at the beginning of the next line.