2011-08-18 36 views
0

我想讀取文本文件看起來像這樣我掃描器類是不讀從.txt文件

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 = null。我可以用什麼方法繼續閱讀下一行?

在此先感謝您提供的任何幫助。

下面的代碼:

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; 

} 

}

回答

4

我會檢查時hasNextInt()返回false的情況下,並添加以下內容:

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

這將消耗的休息並將掃描儀設置在下一行的開頭。

+0

這聽起來很完美,但我不知道該把它放在哪裏,因爲我想用scanner.hasNextInt()來運行它,但我確信在for循環中不會有「ifs」。你會建議什麼? – Jacko85

+0

對不起,忽略了最後一個評論,那是我只是有點愚蠢。這很好,謝謝你。 – Jacko85