2015-10-17 84 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]); 
    } 
} 
相关问题