-2

Right now I'm doing a graph using the library of GraphStream. I know the basics and all, but now I want to do a graph from a .txt.

This is my file:

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

This is my code:

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();
       }
   }

And for some odd reason this is my output:

C1: ATL
C2: TGU
AL: Delta
PR: 358.10
DT: 2208.623
TGU
All: ATL TGU Delta 358.10 2208.623
TGU 
NEW LINE

Why is it printing TGU two times? Also, why it "dies" after the sout of NEW LINE is it because of Double.parseDouble() ?

RMorazan
  • 3
  • 2
  • I would say that's not the code executed, can you post a [mcve]? –  Feb 28 '17 at 19:11
  • Also note that you *maybe* should consider using some CVS parser –  Feb 28 '17 at 19:12
  • s/CVS/CSV/ (Though I'd probably only leverage a third-party CSV parser to get some specific value, like streaming.) –  Feb 28 '17 at 19:25

1 Answers1

4

Problem is with line sc.useDelimiter(",");. You are setting comma as a separator, so fifth string is "2208.623\n TGU" and this string cause java.lang.NumberFormatException, but your catch clause is empty so you even don't know it.

glw
  • 1,646
  • 1
  • 16
  • 20
  • +1 to not to hide exceptions: at least print it to stderr, more preferred - throw it out if you don't know what to do with it. – lospejos Feb 28 '17 at 19:41
  • Thank you so much! It is my first time reading this type of .txt and totally forgot about `\n`. `sc.useDelimiter(",|\\n");` was the answer of my problem – RMorazan Feb 28 '17 at 20:29