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カードからecgファイルを読み取ります

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

3 に答える 3

1

に設定したときに、区切り文字が行末をまだ検出していませんでしたscanner.useDelimiter(",|\\n");

理由を正確に説明することはできませんが、次のように\rを追加する必要があることがわかりました。

scanner.useDelimiter(",|\\r\\n");
于 2011-08-23T10:13:18.397 に答える
1

おそらくscanner.useDelimiter( "、| \\ n");である必要があります。コンパイラが文字列に「\n」ではなく「\n」を挿入するようにしたいため、コンパイラは\をエスケープ文字として認識します。

于 2011-08-19T15:18:14.223 に答える
0

このようにしてみてください:

public boolean ReadFile(Waveform[] waves) {
    while(scanner.hasNextInt()){
        if(scanner.hasNextInt()){
            ecg [chan] [m] = (short) scanner.nextInt();
        }
        if(scanner.hasNextLine()){
            scanner.nextLine(); 
        }       
    }
}
于 2011-08-19T10:36:29.743 に答える