开发者

Scanner isn't reading the last int on each line because of my delimiter

开发者 https://www.devze.com 2023-03-29 00:50 出处:网络
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

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();
                }
       开发者_如何学JAVA     }
        }
        if (!scanner.hasNextInt())
        {
            if (scanner.hasNextLine())
            {
                scanner.nextLine();
                //scanner.nextInt();
            }
        }
    }


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.


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");


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(); 
        }       
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消