2016-03-22 57 views
2

我有這個文本文件閱讀文本文件列每一個不同的陣列

SFrm EFrm SegAScr Phone 
    0 36 -158051 SIL 
    37 105 -644247 +NONTRANS+ 
    106 109 -96452 l SIL w b 
    110 112 -125055 w l aa i 
    113 115 -150550 aa w 7 i 
    116 118 -146662 7 aa i i 
    119 122 -46757 i 7 d i 
    123 126 -58440 d i SIL e 
    127 146 -90776 +MUSIC+ 
    147 152 -61098 t SIL u b 
    153 158 -67393 u t f i 
    159 174 -251284 f u f i 
    175 178 -79772 f f aa i 
    179 194 -134562 aa f 7 i 
    195 206 -33695 7 aa a i 
    207 223 -194024 a 7 SIL e 
    224 350 -434997 +NOISE+ 
    351 353 -28280 SIL 
Total score: -2802095 

我設法存儲在一個字符串這整個事情,但我需要將其存儲在一些陣列,其中每一列代表在另一個數組中,我知道我可以使用.split()作爲過程將其轉換爲數組,但我不能放棄列之間的空格。

PS:文本文件是通用的,所以這些數字和字母不是常量,但它的形式是恆定的(4列)

我現在主要的問題是捕捉重複的元音,當他們在在第四列中的任何行的開始,並在同一行做一些計算與數字,如果有人有更簡單的方法比我的任何幫助,將不勝感激:)

+2

您需要逐行讀取文件,拆分行並將值存儲在它們各自的數組中。 – Atri

+0

你的文件數據中的空間是真正的混亂,有沒有什麼辦法可以改變數據的格式謊言csv。否則它會很難分裂。 – denis

+0

這些列固定寬度嗎? – fateddy

回答

0

我希望這幫助:

public static void main(String[] args) throws FileNotFoundException, IOException { 

      FileReader inputFile = new FileReader("input"); 

      //Instantiate the BufferedReader Class 
      BufferedReader bufferReader = new BufferedReader(inputFile); 

      //Variable to hold the one line data 
      String line="";int index=0; 

      String[] column1= new String[100]; 
      String[] column2 = new String[100]; 
      String[] column3=new String[100]; 
      String[] column4=new String[100]; 



      while ((line = bufferReader.readLine()) != null){ 

       String temp="";int count=1; 
       column4[index]=""; 
       //System.out.println(line); 
       StringTokenizer st = new StringTokenizer(line," "); 
       //String tokenizer gets the token from each space 
       while(st.hasMoreTokens()) 
       { 

        temp = st.nextToken(); 
        //System.out.println(temp); 
        If(temp.equals("Total")){ 
         break; 
        } 

        if(count==1) 
        { 
       // System.out.println(temp); 
        column1[index] = temp; 
        } 
       if(count==2){ 
        column2[index] = temp; 
        } 
       if(count==3) 
       { 
        column3[index] = temp; 
       } 
       if(count==4) 
       { 
       column4[index] += temp; 
       } 
       if(count<4) 
        count++; 
       } 


      index++;    
      } 

      for(int i=0;i<index-1;i++){ 
       System.out.println(column1[i]+" "+column2[i]+" "+column3[i]+" "+column4[i]); 
      } 

     } 

我聲明瞭四個數組來存儲上述數據的列,只要你想。我正在使用stringTokenizer來獲取每個字符串的標記。當我從上面的數組中打印數據時,我得到了這個輸出:

SFrm EFrm SegAScr Phone 
    0 36 -158051 SIL 
    37 105 -644247 +NONTRANS+ 
    106 109 -96452 lSILwb 
    110 112 -125055 wlaai 
    113 115 -150550 aaw7i 
    116 118 -146662 7aaii 
    119 122 -46757 i7di 
    123 126 -58440 diSILe 
    127 146 -90776 +MUSIC+ 
    147 152 -61098 tSILub 
    153 158 -67393 utfi 
    159 174 -251284 fufi 
    175 178 -79772 ffaai 
    179 194 -134562 aaf7i 
    195 206 -33695 7aaai 
    207 223 -194024 a7SILe 
    224 350 -434997 +NOISE+ 
    351 353 -28280 SIL 
+0

我真的很感謝你的努力工作,謝謝那個人,但我沒有澄清我的4列我的壞! – Beeee

+0

您可以在第四列的代碼中進行修改。 – denis

+0

我真的很感謝你的努力工作謝謝你,但我沒有澄清我的4列我的壞!我的4列是(SFrm - > 351)第二個是(Efrm - > 353)。第三個是(SegAScr - > -28280),最後第四個是(Phone - > SIL)Discarding Total score row which which first first word只在你的實現中有一個數組。我真的很抱歉我的錯誤澄清,並再次感謝您的幫助,我會盡力修改您的要求以實現我所需要的。謝謝 – Beeee