title: Input using scanner class in java
body: <p>I was making a program to reduce given integers to their simplest ratio.But an error is occurring while taking inputs through Scanner class in a sub-method of program.Here is the code :</p>
  
  <pre><code>package CodeMania;
  
  import java.util.Scanner;
  
  public class Question5 
  {
  public static void main(String args[])
  {
      Scanner sc=new Scanner(System.in);
      int T=sc.nextInt();// number of test cases
      sc.close();
      if(T&lt;1)
      {
          System.out.println("Out of range");
          System.exit(0);
      }
      for(int i=0;i&lt;T;i++)
      {
      ratio();//line 19
      }
  
  }
  static void ratio()
  {
      Scanner sc1=new Scanner(System.in);
      int N=sc1.nextInt();//line 26
      if((N&gt;500)||(N&lt;1))
      {
          System.out.println("Out of range");
          System.exit(0);
      }
      int a[]=new int[N];
      for(int i=0;i&lt;N;i++)
      {
          a[i]=sc1.nextInt();
      }
      int result = a[0];
     for(int i = 1; i &lt; a.length; i++)
          {
      result = gcd(result, a[i]);
      }
      for(int i=0;i&lt;N;i++)
      {
          System.out.print((a[i]/result)+" ");
      }
      sc1.close();
  }
  static int gcd(int a, int b)
  {
      while (b &gt; 0)
      {
          int temp = b;
          b = a % b;
          a = temp;
      }
      return a;
  }
  }
  </code></pre>
  
  <p>The error is--</p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException
      at java.util.Scanner.throwFor(Scanner.java:862)
      at java.util.Scanner.next(Scanner.java:1485)
      at java.util.Scanner.nextInt(Scanner.java:2117)
      at java.util.Scanner.nextInt(Scanner.java:2076)
      at CodeMania.Question5.ratio(Question5.java:26)
      at CodeMania.Question5.main(Question5.java:19)
  </code></pre>
  
  <p>Here I have used 2  seperate scanner objects sc in main function and sc1 in ratio function to take input from console.
  However if I am declaring a public static type Scanner object in class scope and then using only one Scanner object throughout the program to take input then program is working as required without error.</p>
  
  <p>Why this is happening...?</p>