title: How do I close a scanner in java?
body: <p>When ever I run the following code, </p>
  
  <pre><code>private static String input(String Message){
      Scanner input_scanner = new Scanner(System.in);
      System.out.print("\n" + Message);
      String string = input_scanner.nextLine();
      input_scanner.close();
      return string;
  }
  </code></pre>
  
  <p>I get this error: </p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException: No line found
      at java.util.Scanner.nextLine(Unknown Source)
      at main.learn.input(learn.java:25)
      at main.learn.main(learn.java:13)
  </code></pre>
  
  <p>I figured out it was something to do with the line <code>input_scanner.close();</code> but when I remove it, I get warnings saying:</p>
  
  <blockquote>
    <p>Resource leak: "input_scanner" is never closed"</p>
  </blockquote>
  
  <p>Is there anyway I can stop the errors from happening and get rid of the warnings?</p>