我的加载文件方法不读取整个文本文件

时间:2017-02-28 18:53:12

标签: java graphstream

现在我正在使用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()之后“死”?

1 个答案:

答案 0 :(得分:4)

问题在于行$(document).on('click', '.chbo', function() { this.value ^= 1; console.log(this.value); }); 。您将逗号设置为分隔符,因此第五个字符串为sc.useDelimiter(",");,此字符串会导致"2208.623\n TGU",但您的catch子句为空,因此您甚至不知道它。