-2

现在我正在使用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两次?另外,为什么它在sout之后“死”NEW LINE是因为Double.parseDouble()

4

1 回答 1

4

问题出在 line 上sc.useDelimiter(",");。您将逗号设置为分隔符,所以第五个字符串是"2208.623\n TGU"这个字符串 cause java.lang.NumberFormatException,但是您的 catch 子句是空的,所以您甚至都不知道它。

于 2017-02-28T19:25:31.210 回答