现在,我正在使用GraphStream
库进行图形。我知道基础知识和所有知识,但是现在我想从.txt。
这是我的文件:
ATL,TGU,Delta,358.10,2208.623
TGU,ATL,Delta,488.96,2208.623
TGU,SAP,Avianca,102.20,182.226
TGU,RTB,Avianca,115.80,260.406
RTB,SAP,Avianca,88.40,180.129
SAP,TGU,Avianca,102.20,182.226
这是我的代码:
public void loadFile() {
Scanner sc = null;
this.cities = new ArrayList();
try {
sc = new Scanner(f);
sc.useDelimiter(",");
String city1, city2, airline, dist, pri;
double distance, price;
this.graph.setStrict(false);
this.graph.setAutoCreate(true);
while(sc.hasNext()) {
/*
Souts were used for debugging
*/
city1 = sc.next();
System.out.println("C1: "+city1);
city2 = sc.next();
System.out.println("C2: "+city2);
airline = sc.next();
System.out.println("AL: "+airline);
pri = sc.next();
System.out.println("PR: "+pri);
dist = sc.next();
System.out.println("DT: "+dist);
System.out.println("All: "+city1+" "+city2+" "+airline+" "+pri+" "+dist+" ");
System.out.println("NEW LINE");
distance = Double.parseDouble(dist);
price = Double.parseDouble(pri);
//For some reason the next sout doesn't print distance
System.out.println(distance);
//Verifies if there is a city in the ArrayList
if(!this.cities.contains(city1)){
this.cities.add(city1);
System.out.println("here i am");
}
if(!this.cities.contains(city2)){
this.cities.add(city2);
}
//Adds new edge
graph.addEdge(city1+" a "+city2,city1, city2);
Edge edge = graph.getEdge(city1+" a "+city2);
//Adds attributes to the new edge
System.out.println("Edge: "+this.graph.getEdge(0));
edge.addAttribute("Airline", airline);
edge.addAttribute("Distance", distance);
edge.addAttribute("Price", price);
}
} catch (Exception e) {
} finally {
sc.close();
}
}
出于某种奇怪的原因,这是我的输出:
C1: ATL
C2: TGU
AL: Delta
PR: 358.10
DT: 2208.623
TGU
All: ATL TGU Delta 358.10 2208.623
TGU
NEW LINE
为什么要两次打印TGU
?另外,为什么它在NEW LINE
之后"死亡"是因为Double.parseDouble()
?
问题是线sc.useDelimiter(",");
。您将逗号设置为分隔符,因此第五字符串是"2208.623n TGU"
,此字符串导致java.lang.NumberFormatException
,但是您的捕获子句为空,因此您甚至不知道。