title: Using a java.util.Scanner from within a method causes a runtime error
body: <p>Why would the following code give me a runtime error? It gives me a <code>NoSuchElementException</code> when the method is called the second time. It works fine if I delete the call to <code>stdin.close()</code>, but then Eclipse gives a warning that the resource is not closed.</p>
  
  <h2>Code:</h2>
  
  <pre><code>import java.util.Scanner;
  
  
  public class st {
    public static void main(String[] args) {
      System.out.println("First call");
      getInt("Enter first int (1-10): ");
      System.out.println("Second call");
      getInt("Enter second int (1-10): ");
    }
  
    /**
     * Reads an integer number from the keyboard.
     *
     * @param  prompt    the string to display as a prompt.
     * @return           the integer number entered at the keyboard.
     */
    public static int getInt(String prompt) {
      Scanner stdin = new Scanner(System.in);
  
      System.out.print(prompt);
      int val = stdin.nextInt();
  
      stdin.close();
      return(val);
    }
  }
  
  // END
  </code></pre>
  
  <h2>Output:</h2>
  
  <pre><code>&gt;java st
  First call
  Enter first int (1-10): 5
  Second call
  Enter second int (1-10): Exception in thread "main" java.util.NoSuchElementException
          at java.util.Scanner.throwFor(Unknown Source)
          at java.util.Scanner.next(Unknown Source)
          at java.util.Scanner.nextInt(Unknown Source)
          at java.util.Scanner.nextInt(Unknown Source)
          at st.getInt(st.java:24)
          at st.main(st.java:9)
  
  &gt;
  </code></pre>
  
  <h2>Version:</h2>
  
  <pre><code>&gt;java -version
  java version "1.7.0_21"
  Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
  Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
  
  &gt;
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Brian</p>