由于我的分隔符,扫描仪无法读取每行的最后一个 int

发布于 2024-11-30 04:29:17 字数 1881 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(3

压抑⊿情绪 2024-12-07 04:29:18

当设置为 scanner.useDelimiter(",|\\n"); 时,分隔符仍然没有拾取行尾;

我无法准确解释原因,但事实证明我需要添加\r 也在那里:

scanner.useDelimiter(",|\\r\\n");

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:

scanner.useDelimiter(",|\\r\\n");
撑一把青伞 2024-12-07 04:29:18

尝试这样:

public boolean ReadFile(Waveform[] waves) {
    while(scanner.hasNextInt()){
        if(scanner.hasNextInt()){
            ecg [chan] [m] = (short) scanner.nextInt();
        }
        if(scanner.hasNextLine()){
            scanner.nextLine(); 
        }       
    }
}

Try like this:

public boolean ReadFile(Waveform[] waves) {
    while(scanner.hasNextInt()){
        if(scanner.hasNextInt()){
            ecg [chan] [m] = (short) scanner.nextInt();
        }
        if(scanner.hasNextLine()){
            scanner.nextLine(); 
        }       
    }
}
冷弦 2024-12-07 04:29:17

应该是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.

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