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

发布于 2024-11-30 17:16:50 字数 1502 浏览 0 评论 0原文

我正在尝试读取一个如下所示的文本文件

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-12-07 17:16:50

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文