2017-02-22 118 views
0

我想這兩個文本文件合併合併兩個數組列表到一個TreeMap在java中

驅動程序詳細信息的文本文件:

AB11; Angela 
AB22; Beatrice 

旅程文本文件:

AB22,Edinburgh ,6 
AB11,Thunderdome,1 
AB11,Station,5 

而且我想我的輸出只是名字和人物的位置。它應該看起來像這樣:

Angela 
    Thunderdone 
    Station 

Beatrice 
    Edinburgh 

這是我的代碼。我不知道我做錯了什麼,但我沒有得到正確的輸出。

ArrayList<String> names = new ArrayList<String>(); 
TreeSet<String> destinations = new TreeSet<String>(); 

public TaxiReader() { 

    BufferedReader brName = null; 
    BufferedReader brDest = null; 

    try { 

     // Have the buffered readers start to read the text files 
     brName = new BufferedReader(new FileReader("taxi_details.txt")); 
     brDest = new BufferedReader(new FileReader("2017_journeys.txt")); 

     String line = brName.readLine(); 
     String lines = brDest.readLine(); 

     while (line != null && lines != null){ 

      // The input lines are split on the basis of certain characters that the text files use to split up the fields within them 
      String name [] = line.split(";"); 
      String destination [] = lines.split(","); 

      // Add names and destinations to the different arraylists 
      String x = new String(name[1]); 
      //names.add(x); 

      String y = new String (destination[1]); 
      destinations.add(y); 


      // add arraylists to treemap 
      TreeMap <String, TreeSet<String>> taxiDetails = new TreeMap <String, TreeSet<String>>(); 
      taxiDetails.put(x, destinations); 

      System.out.println(taxiDetails); 

      // Reads the next line of the text files 
      line = brName.readLine(); 
      lines = brDest.readLine(); 

     } 

    // Catch blocks exist here to catch every potential error 
    } catch (FileNotFoundException ex) { 
      ex.printStackTrace(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     // Finally block exists to close the files and handle any potential exceptions that can happen as a result 
     } finally { 
      try { 
       if (brName != null) 
        brName.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

} 


public static void main (String [] args){ 
    TaxiReader reader = new TaxiReader(); 
} 
+3

歡迎堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

+1

可能想查看['BufferedReader#lines()'](http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#lines--)和[try-用資源(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) –

回答

1

您正在閱讀2個文件並行,我不認爲這會工作得太好。嘗試一次讀取一個文件。

你也可能想重新考慮你的數據結構。

第一個文件將key「AB11」與value「Angela」相關聯。地圖是比一個ArrayList更好:

Map<String, String> names = new HashMap<String, String>(); 

String key = line.split(",")[0]; // "AB11" 
String value = line.split(",")[1]; // "Angela" 
names.put(key, value) 
names.get("AB11"); // "Angela" 

類似地,第二文件涉及一個key 「AB11」 到多個values 「Thunderdome」, 「站」。你也可以使用相關地圖:

Map<String, List<String>> destinations = new HashMap<String, List<String>>(); 

String key = line.split(",")[0]; // "AB11" 
String value = line.split(",")[1]; // "Station" 

if(map.get(key) == null) { 
    List<String> values = new LinkedList<String>(); 
    values.add(value); 
    map.put(key, values); 
} else { 
    // we already have a destination value stored for this key 
    // add a new destination to the list 
    List<String> values = map.get(key); 
    values.add(value); 
} 

爲了得到你想要的輸出:

// for each entry in the names map 
for(Map.Entry<String, String> entry : names.entrySet()) { 
    String key = entry.getKey(); 
    String name = entry.getValue(); 

    // print the name 
    System.out.println(name); 

    // use the key to retrieve the list of destinations for this name 
    List<String> values = destinations.get(key); 
    for(String destination : values) { 
     // print each destination with a small indentation 
     System.out.println(" " + destination); 
    } 
}