2015-10-17 18 views
-1

我有一個名爲讀數txt文件,它有它的以下數據:如何在Java中的.txt文件的同一行上用兩個分隔符分割文本?

-10,3NW,15cm,4:38 
5,15SW,8mm,2:8 
8,8ENE,2mm,:25 
-5,0,7cm,1 
-3,0,3mm 

凡第一位置代表的溫度,速度,沉澱和時間(小時和分鐘)

我想僅當第四個標記存在時,纔將字符串拆分爲tokens = line.split(":");。 我對分割字符串而不做任何分裂與分隔符:代碼:

try { 
     input = new BufferedReader(new FileReader("readings.txt")); 

     line = input.readLine(); 
     while (line != null) { 
      tokens = line.split(","); 

      temperature = Integer.parseInt(tokens[0].trim()); 

      tokens[1] = tokens[1].trim(); 
      separation = firstNonNumericPosition(tokens[1]); 




      if (separation == 0 || (separation < 0 && Integer.parseInt(tokens[1]) != 0)) { 
       speed = -1; 
      } else { 
       if (separation < 0) { 
        speed = 0; 
        direction = ""; 
       } else { 
        numeric = tokens[1].substring(0, separation); 
        speed = Integer.parseInt(numeric.trim()); 
        direction = tokens[1].substring(separation).trim(); 
       } 

       if (tokens.length > 2) { 
        tokens[2] = tokens[2].trim(); 
        separation = firstNonNumericPosition(tokens[2]); 
        if (separation <= 0) { 
         precipitation = -1; 
        } else { 
         numeric = tokens[2].substring(0, separation); 
         precipitation = Integer.parseInt(numeric.trim()); 
         unit = tokens[2].substring(separation).trim(); 
        } 
       } else { 
        precipitation = 0; 
        unit = ""; 
       } 

      } 


      if (speed < 0 || precipitation < 0) { 
       System.out.println("Error in input: " + line); 
      } else { 
       readings[size] = new Reading(temperature, speed, direction, 
         precipitation, unit.equalsIgnoreCase("cm")); 
       size++; 
      } 




      line = input.readLine(); 
     } 

     input.close(); 



    } catch (NumberFormatException ex) { 
     System.out.println(ex.getMessage()); 
    } catch (IOException ioe) { 
     System.out.println(ioe.getMessage()); 
    } catch (ArrayIndexOutOfBoundsException ar){ 
     System.out.println(ar.getMessage()); 
    } 

我試圖用這個邏輯,但它給了3

if(tokens.length > 3) { 
    tokens = line.split(":"); 
    hours =Integer.parseInt(tokens[3].trim()); 
    minutes =Integer.parseInt(tokens[4].trim()); 
} 

的ArrayIndexOutOfBoundException怎麼可能分裂它是否存在第四個標記?

這些只是我的代碼的一部分,可以提供任何關於問題意味着什麼(如果我不夠清楚)的進一步解釋。提前致謝!

+0

你爲什麼不通過逗號分割? – michaelsnowden

+0

我也用逗號分開。它在第六行@michaelsnowden的代碼中 – ekeith

回答

0

使用StringTokenizer類。更容易處理令牌。

StringTokenizer tokens = new StringTokenizer(line,":"); 

可以輕鬆地檢查:

string firstToken = tokens.nextToken(); //capture out first token 
if(tokens.hasMoreTokens()){ //check if 2nd token is there 
    StringTokenizer subTokens = new StringTokenizer(firstToken, ","); 
} 
0
if(tokens.length > 3) { 
    tokens = line.split(":"); 
    hours =Integer.parseInt(tokens[3].trim()); 
    minutes =Integer.parseInt(tokens[4].trim()); 
} 

我覺得你的意思寫的是這樣的:

if (tokens.length > 3) { 
    String[] time = tokens[3].split(":"); 
    String hoursString = time[0]; 
    if (hoursString.length() > 0) { 
     hours = Integer.parseInt(hoursString); 
    } 
    if (time.length > 1) { 
     minutes = Integer.parseInt(time[1]); 
    } 
} 
相關問題