由于我的分隔符,扫描仪无法读取每行的最后一个 int
我正在尝试使用 Scanner 类读取如下所示的简单文本文件,并将分隔符设置为 scanner.useDelimiter(",");
但是,如您所见,末尾没有逗号每行的最后一个数字,这样扫描仪就不会读取每行的最后一个数字。有人可以建议如何解决这个问题吗?
预先感谢您的任何帮助。
文本文件:
0,4,4,0,-4,2,2,8,16,20,20,12,8
1,6,7,1,-6,4,2,6,12,19,22,12,8
2,6,8,2,-7,5,2,4,11,19,23,14,8
3,4,8,4,-6,6,0,3,11,20,24,15,8
4,4,7,3,-5,5,0,0,12,20,24,16,10
这也是我的代码: public class ECGFilereader { // 从SD卡读取心电图文件
public final static int numChannels = 12; // the data is stored in 12 channels, one for each lead
public final static int numSamples = 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() +"/1009856.txt"); //accesses the ecg file from the SD card
scanner = new Scanner(file);
scanner.useDelimiter(",");
}
public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 5000 samples)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++) //
{
scanner.nextInt();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++) //&& scanner.hasNextInt()
{
ecg [chan] [m] = (short) scanner.nextInt();
if (!scanner.hasNextInt())
{
if (scanner.hasNextLine())
{
scanner.nextLine();
//scanner.nextInt();
}
}
}
if (!scanner.hasNextInt())
{
if (scanner.hasNextLine())
{
scanner.nextLine();
//scanner.nextInt();
}
}
}
I'm trying to read a simple text file shown below with the Scanner class and have a a delimiter set as scanner.useDelimiter(",");
however as you can see there is no comma at the end of each line so the scanner doesn't read the last number on each line. Can anybody suggest how to solve this problem?
Thanks in advance for any help.
text file:
0,4,4,0,-4,2,2,8,16,20,20,12,8
1,6,7,1,-6,4,2,6,12,19,22,12,8
2,6,8,2,-7,5,2,4,11,19,23,14,8
3,4,8,4,-6,6,0,3,11,20,24,15,8
4,4,7,3,-5,5,0,0,12,20,24,16,10
here's my code too:
public class ECGFilereader { // reads the ecg files from the SD card
public final static int numChannels = 12; // the data is stored in 12 channels, one for each lead
public final static int numSamples = 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() +"/1009856.txt"); //accesses the ecg file from the SD card
scanner = new Scanner(file);
scanner.useDelimiter(",");
}
public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 5000 samples)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++) //
{
scanner.nextInt();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++) //&& scanner.hasNextInt()
{
ecg [chan] [m] = (short) scanner.nextInt();
if (!scanner.hasNextInt())
{
if (scanner.hasNextLine())
{
scanner.nextLine();
//scanner.nextInt();
}
}
}
if (!scanner.hasNextInt())
{
if (scanner.hasNextLine())
{
scanner.nextLine();
//scanner.nextInt();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当设置为
scanner.useDelimiter(",|\\n");
时,分隔符仍然没有拾取行尾;我无法准确解释原因,但事实证明我需要添加\r 也在那里:
The delimiter wasn't picking up the end of line still when set to
scanner.useDelimiter(",|\\n");
I can't explain why exactly but it turns out I needed to add \r in there too like:
尝试这样:
Try like this:
应该是scanner.useDelimiter(",|\\n");
因为您希望编译器将“\n”而不是“\n”放入字符串中,并且编译器会将 \ 视为转义字符。
Should it perhaps be scanner.useDelimiter(",|\\n");
Because you want the compiler to put "\n" in the string rather than '\n' and the compiler will see the \ as an escape character.