title: Help with traversing through node/ input file read
body: <p>So I have this assignment where I read in 1 line at a time separated by comma e.g.</p>
  
  <pre><code>Atlanta, Philadelphia   
  New York, Philadelphia   
  Philadelphia, Chicago   
  Washington, Florida
  .....
  up to a vast amount.. (I don't know the amount)
  </code></pre>
  
  <p>Each line represents connectivity between the two locations (e.g. Atlanta connects to Philadelphia)  creating connected nodes and nodes which are not connected like Washington and Florida is connected to each other but no one else.</p>
  
  <p>What the program is suppose to do is read the file and given two city arguments its suppose to spit out Yes if its connected/ No if its not.</p>
  
  <p>I finished my program and It works, however its not efficient. I'm stumped as to what I can do. Here is part of the program which makes the code inefficient.</p>
  
  <p>This first input reads the file so I can determine the size of the list of different city, and it also removes any duplicate cities.</p>
  
  <pre><code>private static void createCityList() throws IOException{
  
          try {
              FileReader a = new FileReader(file);
              BufferedReader br = new BufferedReader(a);
              String line;
              line = br.readLine();
  
              while(line != null){
                  StringTokenizer st = new StringTokenizer(line, ",");
                  while(st.hasMoreTokens()){ 
                      String currentToken = st.nextToken();
                      if(!cityList.contains(currentToken.trim())){ 
                          cityList.add(currentToken.trim());
                      }//if
                  }//while hasMoreTokens
                  line = br.readLine();//read the next line
              }//while line != null
              br.close();
          }//try
  
          catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          length = cityList.size(); // set length to amount of unique cities
  
      }//createCityList
  </code></pre>
  
  <p>the 2nd method which does another fileread... allows me to create an adjacency matrix</p>
  
  <pre><code>private static void graph() throws IOException{ 
      cityGraph = new int[cityList.size()][cityList.size()]; 
  
          try {
              FileReader a = new FileReader(file);
              BufferedReader br = new BufferedReader(a);
              String line;
              line = br.readLine();
  
  
              while(line != null){
                  StringTokenizer st = new StringTokenizer(line, ",");
                  while(st.hasMoreTokens()){ 
                      String firstToken = st.nextToken().trim();
                      String secondToken = st.nextToken().trim();
                      cityGraph[cityList.indexOf(firstToken)][cityList.indexOf(secondToken)] = 1; 
                      cityGraph[cityList.indexOf(secondToken)][cityList.indexOf(firstToken)] = 1; 
                  }//while hasMoreTokens
  
                  line = br.readLine();//read the next line
  
              }//while line != null
  
              br.close();
  
          }//try
  
          catch (FileNotFoundException e) {
              e.printStackTrace();
          }//catch
      }//graph
  </code></pre>
  
  <p>And my final method runs a DFS on the 2 cities to determine if its connected</p>
  
  <pre><code>private static void isConnected(String s1, String s2){
  
          city1 = cityList.indexOf(s1); //set city to the index of s1 or s2 in the cityList LinkedList.
          city2 = cityList.indexOf(s2); 
  
  
          int startNode = city1;
          q.add(startNode); // start node
  
          while(!q.isEmpty()){
          //visit vertex
              for(int i = 0; i &lt; length; i++){
                  if(cityGraph[startNode][i] == 1){
                      if( i == city2 ){ 
                          System.out.println("yes");
                          return;
                      }//if city2 found
                      q.add(i);
                      cityGraph[startNode][i] = 0; //Set to visited
                  }//if vertex exist
              }//for
              q.remove();//remove the top element and start with new node
              if(!q.isEmpty()){
                  startNode = (Integer) q.element();
              }//if
  
          }//while q is not empty     
          System.out.println("no");
      }//isConnected
  </code></pre>
  
  <p>I'm trying to only have one file read, but I'm having issues making a matrix from an unknown size its only after the file read that I find out the size. Any help or suggestion would be greatly 
  appreciated! </p>