0

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();
            }
        }
    }
Jacko85
  • 153
  • 4
  • 12

3 Answers3

1

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.

Juxon
  • 11
  • 1
  • I tried this and it makes my file readed take every other int and put it in the right place now however it still won't read the last int from the line so the arrya for the last line remains as 0,0,0,0,0... – Jacko85 Aug 22 '11 at 10:30
1

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");
Jacko85
  • 153
  • 4
  • 12
0

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(); 
        }       
    }
}
jainal
  • 2,973
  • 2
  • 20
  • 18