2015-08-28 77 views
2

可以說我有這有兩個字符串中的每一行的文本文件:如何檢查文本文件是否包含單個字符串或兩個字符串?

New York 52.523405 13.4114 
San Antonio 41.387917 2.169919 
Los Angeles 51.050991 13.733634 

,這是我的代碼從線路輸出分割字符串:

for (int i = 0; i < noOfStores;i++){ 
    nextLine = console.readLine(); 
    nextLine = nextLine.trim(); 
    String temp[] = nextLine.split(" "); 
    String Word = temp[0] + " " + temp[1]; 
    storeNames[i] = firstWord; 
    latitudes[i] = Double.parseDouble(temp[2]); 
    longitudes[i] = Double.parseDouble(temp[3]); 
} 

但如果一個文本文件只包含一個字符串在這樣的每一行:

Berlin 52.523405 13.4114 
Barcelona 41.387917 2.169919 
Dresden 51.050991 13.733634 

如何檢查一個文本文件中是否包含一個或閱讀時有兩個string?

+0

請參閱更新的答案,包括代碼。 –

回答

2

使用split(" "),得到返回數組的長度,然後解析數組中的最後兩個字符串數組項,項目length - 1length - 2,雙打,然後通過剩餘的字符串項目前的最後兩個項目進行迭代,並結合他們作爲城市字符串。類似的,

for (int i = 0; i < noOfStores;i++){ 
    nextLine = console.readLine(); 
    nextLine = nextLine.trim(); 
    String temp[] = nextLine.split(" "); 
    int length = temp.length; 
    if (length < 3) { 
     // output is not as expected; throw some type of exception here. 
    } 
    latitudes[i] = Double.parseDouble(temp[length - 2]); 
    longitudes[i] = Double.parseDouble(temp[length - 1]); 

    // this should handle city names with 1, 2 or any number of tokens 
    StringBuilder wordSb = new StringBuilder(); 
    for (int j = 0; j < length - 2; j++) { 
     wordSb.append(temp[j]); 
     if (j != length - 3) { 
      wordSb.append(" "); 
     } 
    } 
    storeNames[i] = wordSb.toString(); 
} 
1

使用正則表達式。

String testData = "New York 52.523405 13.4114\n" + 
        "San Antonio 41.387917 2.169919\n" + 
        "Los Angeles 51.050991 13.733634\n" + 
        "Berlin 52.523405 13.4114\n" + 
        "Barcelona 41.387917 2.169919\n" + 
        "Dresden 51.050991 13.733634"; 

Pattern p = Pattern.compile("\\s*(.*?)\\s+(-?[0-9.]+)\\s+(-?[0-9.]+)\\s*"); 
try (BufferedReader in = new BufferedReader(new StringReader(testData))) { 
    String line; 
    while ((line = in.readLine()) != null) { 
     Matcher m = p.matcher(line); 
     if (! m.matches()) 
      throw new IllegalArgumentException("Bad data: " + line); 
     String storeName = m.group(1); 
     double latitude = Double.parseDouble(m.group(2)); 
     double longitude = Double.parseDouble(m.group(3)); 
     System.out.printf("Store '%s' is at %f, %f%n", storeName, latitude, longitude); 
    } 
} 

輸出

Store 'New York' is at 52.523405, 13.411400 
Store 'San Antonio' is at 41.387917, 2.169919 
Store 'Los Angeles' is at 51.050991, 13.733634 
Store 'Berlin' is at 52.523405, 13.411400 
Store 'Barcelona' is at 41.387917, 2.169919 
Store 'Dresden' is at 51.050991, 13.733634 
1

你的任務最合適的工具是正則表達式。 由於您可以確定您的兩個數字不包含任何空格,因此您可以將它們定義爲"\S+",並通過name的模式保留其他任何內容。

這允許您在name部分中擁有任意數量的單詞(以及字面意義上的任何其他內容),同時允許以任何格式(如科學記數法)編號,只要它們沒有空格。

String[] lines = new String[]{ 
     "New York 52.523405 13.4114", 
     "San Antonio 41.387917 2.169919", 
     "Los Angeles 51.050991 13.733634", 
     "Berlin 52.523405 13.4114", 
     "Barcelona 41.387917 2.169919", 
     "Dresden 51.050991 13.733634", 
     "Some scientific notation 1E-4 13.733634" 
}; 

Pattern pattern = Pattern.compile("(.*)\\s+(\\S+)\\s+(\\S+)"); 

for (String line : lines) { 

    Matcher matcher = pattern.matcher(line); 
    if (matcher.matches()) { 
     String name = matcher.group(1); 
     double latitude = Double.parseDouble(matcher.group(2)); 
     double longitude = Double.parseDouble(matcher.group(3)); 

     System.out.printf("'%s', %.4f %.4f\n", name, latitude, longitude); 
    } 
} 

結果:

'New York', 52.5234 13.4114 
'San Antonio', 41.3879 2.1699 
'Los Angeles', 51.0510 13.7336 
'Berlin', 52.5234 13.4114 
'Barcelona', 41.3879 2.1699 
'Dresden', 51.0510 13.7336 
'Some scientific notation', 0.0001 13.7336 
相關問題