title: Write code to simulate Depth First Search (DFS) by reading from File using Java
body: <p>Write code to simulate Depth First Search (DFS) by reading <code>FinalQ1Input.txt</code> using Java File I/O, and the starting vertex is 0.</p>
  
  <p>The output should look like the following:</p>
  
  <p><img src="https://i.stack.imgur.com/JjEpF.png" alt="enter image description here"></p>
  
  <p><code>FinalQ1Input.txt</code> values are here:</p>
  
  <p><img src="https://i.stack.imgur.com/FpJ6m.png" alt="enter image description here"></p>
  
  <p>And my code is:</p>
  
  <pre><code>import java.io.*; 
  import java.util.*; 
  
  
       // Ref --&gt; https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
  
      // This class represents a directed graph using adjacency list 
      // representation 
      class SP2020_CSE373_Final_Q2_1234567 
      { 
          private int V;   // No. of vertices 
  
          // Array  of lists for Adjacency List Representation 
          private LinkedList&lt;Integer&gt; adj[]; 
  
          // Constructor 
          SP2020_CSE373_Final_Q2_1234567(int v) 
          { 
              V = v; 
              adj = new LinkedList[v]; 
              for (int i=0; i&lt;v; ++i) 
                  adj[i] = new LinkedList(); 
          } 
  
          //Function to add an edge into the graph 
          void addEdge(int v, int w) 
          { 
              adj[v].add(w);  // Add w to v's list. 
          } 
  
          // A function used by DFS 
          void DFSUtil(int v,boolean visited[]) 
          { 
              // Mark the current node as visited and print it 
              visited[v] = true; 
              System.out.print(v+" "); 
  
              // Recur for all the vertices adjacent to this vertex 
              Iterator&lt;Integer&gt; i = adj[v].listIterator(); 
              while (i.hasNext()) 
              { 
                  int n = i.next(); 
                  if (!visited[n]) 
                      DFSUtil(n, visited); 
              } 
          } 
  
          // The function to do DFS traversal. It uses recursive DFSUtil() 
          void DFS(int v) 
          { 
              // Mark all the vertices as not visited(set as 
              // false by default in java) 
              boolean visited[] = new boolean[V]; 
  
              // Call the recursive helper function to print DFS traversal 
              DFSUtil(v, visited); 
          } 
  
          public static void main(String args[]) throws IOException
          { 
              System.out.print("15 32 \n");
              System.out.println("Vertices : \n { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \n  10, 11, 12, 13, 14 }");
              System.out.println("Edges : \n { (0, 1), (0, 2), (1, 3), (2, 3), (1, 6),\n"
                      + "(1, 3), (6, 3), (2, 3), (3, 7), (2, 7),\n"
                      + "(6, 4), (3, 4), (3, 5), (5, 7), (4, 5),\n"
                      + "(4, 9), (6, 9), (9, 10), (4, 10), (5, 10),\n"
                      + "(5, 11), (10, 11), (7, 11), (9, 10), (10, 11),\n"
                      + "(10, 12), (10, 13), (9, 14), (11, 14), (1, 14),\n"
                      + "(2, 14) }");
              System.out.print("Enter the source vertex: ");
              Scanner input = new Scanner(System.in);
              int a = input.nextInt();
  
               BufferedReader inFile;
               inFile = new BufferedReader(new FileReader("C:/Users/hp/eclipse-workspace/Final_Assignment/src/SP2020_CSE373_Final_Q2_1234567/FinalQ1Input.txt"));
               File file = new File("C:/Users/hp/eclipse-workspace/Final_Assignment/src/SP2020_CSE373_Final_Q2_1234567/FinalQ1Input.txt");
               Scanner fIn = new Scanner(file);
               fIn.nextLine(); //skipping the first line!
               while (fIn.hasNext())
               {
                  // System.out.print(fIn.next());
                   System.out.print(" Visited Vertex:  ");
                   System.out.print(fIn.nextInt());
                   System.out.print(" Parents:  ");
                   System.out.println(fIn.nextBigInteger());
               }
               fIn.close();
  
  
  
  
  
  
              // SP2020_CSE373_Final_Q2_1234567 g = new SP2020_CSE373_Final_Q2_1234567(4); 
  
            /*  g.addEdge(0, 1); 
              g.addEdge(0, 2); 
              g.addEdge(1, 2); 
              g.addEdge(2, 0); 
              g.addEdge(2, 3); 
              g.addEdge(3, 3); */
  
           //   System.out.println("Following is Depth First Traversal "+ 
                             //    "(starting from vertex 2)"); 
            //  Scanner input = new Scanner(System.in);
             // g.DFS(2); 
          } 
      } 
  </code></pre>
  
  <p>What's wrong with my code? My output is showing a garbage value.</p>