title: Error while redirecting the input from file
body: <p>Here's my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  class Graph{
  boolean [][]array;
  int N;
  Graph (){
      array = new boolean [1001][1001];
      N=0;
  }
  
  void read_graph() {
      Scanner sc = new Scanner(System.in);
      N = sc.nextInt();
      sc.nextLine();
  
      String str;
      String []substr;
  
      for (int K=1; K&lt;=N; K++){
          str = sc.nextLine();
          substr = str.split(" ");
          for (int I=0; I&lt;substr.length; I++)
              System.out.println(substr[0]+" "+substr[I]);
      }   
  }
  
  void query(){
  Scanner sc = new Scanner(System.in);
      int P, Q;
      int counter = 0;
      boolean flag = true;
      while (flag){
      counter++;
      P = sc.nextInt();
      Q = sc.nextInt();
      sc.nextLine();
      if ( P == Q &amp;&amp; P == 0 )
          flag =false;
      else {
          if (Q == 1)
              System.out.println("DFS done");
          else
              System.out.println("Bfs done");      
                    }
      }
      }       
  }
  class demo{
  public static void main( String [] args ){
      Graph G= new Graph();
      Scanner sc = new Scanner(System.in);
      int numGraphs = sc.nextInt();
      while (numGraphs&gt;0){
          G.read_graph();
          G.query();
          numGraphs--;
          }
      }
  }
  </code></pre>
  
  <p>Here's the Input data:</p>
  
  <pre><code>1
  6
  1 2 3 4
  2 2 3 6
  3 2 1 2
  4 1 1
  5 0
  6 1 2
  5 1
  1 0
  1 0
  0 0
  </code></pre>
  
  <p>When I give this input data with keyboard it works fine but when I saved this input to file and redirected this as input(in linux using '&lt;'), it throws error message.</p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException
  at java.util.Scanner.throwFor(Scanner.java:855)
  at java.util.Scanner.next(Scanner.java:1478)
  at java.util.Scanner.nextInt(Scanner.java:2108)
  at java.util.Scanner.nextInt(Scanner.java:2067)
  at Graph.read_graph(b.java:13)
  at demo.main(b.java:56)
  </code></pre>
  
  <p>Help me in pointing out the mistake.</p>