我的扫描仪类没有从.txt文件中读取下一行

时间:2011-08-18 10:45:30

标签: android line java.util.scanner delimiter text-files

我正在尝试阅读看起来像这样的文本文件

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 = null。我可以用什么方法继续阅读下一行?

提前感谢您提供的任何帮助。

以下是代码:

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;

}

}

1 个答案:

答案 0 :(得分:4)

我会检查hasNextInt()返回false时的情况,并添加以下内容:

if (!scanner.hasNextInt())
{
    if (scanner.hasNextLine())
    {
        scanner.nextLine();
    }
}

这将占用该行的其余部分,并将扫描仪设置为下一行的开头。