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