2017-02-22 91 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); 
    } 
}