title: Converting txt file to adjacency list, then making Graph
body: <p>I am doing a Breadth First Search program that will read in an adjacency list type text file, then perform BFS on it accordingly. I am having trouble reading in the text file, then adding it as an array list of nodes.</p>
  
  <p><em>How would I get it to correctly read each line and associate the connections for it?</em> </p>
  
  <p>I have tested my program by manually adding Nodes within my main, then making a graph and performing BFS on it.</p>
  
  <p>Here is my Node class:</p>
  
  <pre><code>import java.util.*;
  
  public class Node {
  
          public String data; // data element
          public boolean visited=false; // flag to track the already visited node
          public List&lt;Node&gt; adjacentNodes = new LinkedList&lt;Node&gt;(); // adjacency list
  //      public List adjacentNodes = new LinkedList(); // adjacency list
          public Node rootNode;
  
          public Node(String data){
              this.data = data;
          }
  
          public void addAdjacentNode(final Node node){
              adjacentNodes.add(node);
              node.adjacentNodes.add(this);
  //          adjacentNodes.add(rootNode);
  //          node.adjacentNodes.add(this)
          }
  
      }
  </code></pre>
  
  <p>And here is my Graph class: (Where I attempted to read in my text file is within my main)</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.InputStream;
  import java.util.*;
  
  
  /*- enqueue the start node to a Queue
  - make the start node as visited
  - while queue is not empty
    - dequeue the node lets say u
    - print or whatever you want to
    - for every adjacent node v of u
        - if v is not already visited
            - mark v as visited
            - enqueue v to the Queue*/
  public class Graph {
  
      public List nodes = new ArrayList();
  
      public void breadthFirstTraversal(Node rootNode){
          Queue&lt;Node&gt; q = new LinkedList&lt;Node&gt;();
  //      Queue q = new LinkedList();
          q.add(rootNode);
          System.out.print(rootNode.data + " ");
  //      printNode(rootNode);
          rootNode.visited=true;
          while(!q.isEmpty()){
              Node n = (Node)q.poll();
              System.out.print(n.data + " ");
              for(Node adj : n.adjacentNodes){
                  if(!adj.visited){
                      adj.visited=true;
                      q.add(adj);
                  }
              }
              clearNodes();
          }
  
      }
  
      private void clearNodes() {
          // TODO Auto-generated method stub
          nodes = null;   //clear nodes and set to null
      }
  
  /*  private void printNode(Node node) {
          // TODO Auto-generated method stub
          System.out.print(node);
      }*/
  
      public static void main(String[] args) throws FileNotFoundException {
  
          Graph g = new Graph();
  
          Scanner scan = new Scanner(new File("Connections.txt"));   // scanner to read file
          String line = scan.nextLine();                        // read first line
          int nbLine = Integer.parseInt(line);                  // get number of lines
          ArrayList&lt;int[]&gt; al = new ArrayList&lt;int[]&gt;();        
          for(int i = 0; i &lt; nbLine; i++) {                     // read each line
             line = scan.nextLine();
             String[] token = line.split(" ");                  // split each number into different String
             int[] points = new int[token.length - 1];          // prepare array of int[] - 1
  //         int[] point = new int[];
             int[] point = new int[token.length];
          for(int j = 0; j &lt; token.length; j++){              // skip first one
               points[j-1] = Integer.parseInt(token[j]);       // store as int
             al.add(points);                                    // save in ArrayList
          }
  
  
  /*      Scanner s = new Scanner(new File("C:/Users/cantuj3/Documents/Ass 2/Connections.txt"));
          ArrayList&lt;Node&gt; list = new ArrayList&lt;Node&gt;();
          while (s.hasNext()){
              g.nodes.add(s.next());
              //list.add(g);
          }       
          s.close();*/
  
  
  
  /*      Node frankfurt = new Node("frankfurt");
          Node mannheim = new Node("mannheim");
          Node wurzburg = new Node("wurzburg");
          Node stuttgard = new Node("stuttgard");
          Node kassel = new Node("kassel");
          Node karlsruhe = new Node("karlsruhe");
          Node erfurt = new Node("erfurt");
          Node numberg = new Node("numberg");
          Node augsburg = new Node("augsburg");
          Node munchen = new Node("munchen");
  
          Graph g = new Graph();
  
          g.nodes.add(frankfurt);
          g.nodes.add(mannheim);
          g.nodes.add(wurzburg);
          g.nodes.add(stuttgard);
          g.nodes.add(kassel);
          g.nodes.add(karlsruhe);
          g.nodes.add(erfurt);
          g.nodes.add(numberg);
          g.nodes.add(augsburg);
          g.nodes.add(munchen);
  
          frankfurt.addAdjacentNode(mannheim);
          frankfurt.addAdjacentNode(wurzburg);
          frankfurt.addAdjacentNode(kassel);
  
          mannheim.addAdjacentNode(karlsruhe);
  
          karlsruhe.addAdjacentNode(augsburg);
  
          augsburg.addAdjacentNode(munchen);
  
          munchen.addAdjacentNode(kassel);
          munchen.addAdjacentNode(numberg);
  
          wurzburg.addAdjacentNode(erfurt);
          wurzburg.addAdjacentNode(numberg);
  
          numberg.addAdjacentNode(stuttgard);
          g.breadthFirstTraversal(frankfurt);*/
      }
  
  }
  </code></pre>
  
  <p><strong>Here is my input file:</strong></p>
  
  <pre><code>01 02
  02 01 03
  03 02 04 05
  04 03
  05 03 06
  06 05
  </code></pre>
  
  <p>Here is that chunk of code I wrote in my main by itself:</p>
  
  <pre><code>        Scanner scan = new Scanner(new File("Connections.txt"));   // scanner to read file
          String line = scan.nextLine();                        // read first line
          int nbLine = Integer.parseInt(line);                  // get number of lines
          ArrayList&lt;int[]&gt; al = new ArrayList&lt;int[]&gt;();        
          for(int i = 0; i &lt; nbLine; i++) {                     // read each line
             line = scan.nextLine();
             String[] token = line.split(" ");                  // split each number into different String
             int[] points = new int[token.length - 1];          // prepare array of int[] - 1
  //         int[] point = new int[];
             int[] point;
          for(int j = 0; j &lt; token.length; j++){              // skip first one
               points[j-1] = Integer.parseInt(token[j]);       // store as int
             al.add(points);                                    // save in ArrayList
          }
  </code></pre>
  
  <p><strong>Am I on the right track?</strong> </p>