title: Surrounding a Scanner with try-with-resources
body: <p>I've created a an algorithm to read in a file and check multiple questions of user input. I am using Netbeans, and it is suggesting a try-with-resources. What I'm not sure about is the closing of the file. When I first created my algorithm, I put the file.close() in the wrong spot, as it couldn't be reached because there was a "return" statement before it:</p>
  
  <pre><code>while (inputFile.hasNext()) {
          String word = inputFile.nextLine();
          for (int i = 0; i &lt; sentance.length; i++) {
              for (int j = 0; j &lt; punc.length; j++) {
                  if (sentance[i].equalsIgnoreCase(word + punc[j])) {
  
                      return "I am a newborn. Not even a year old yet.";
                  }
              }
          }
      }
      inputFile.close(); // Problem
  </code></pre>
  
  <p>So i fixed it with this:</p>
  
  <pre><code>        File file = new File("src/res/AgeQs.dat");
      Scanner inputFile = new Scanner(file);
      while (inputFile.hasNext()) {
          String word = inputFile.nextLine();
          for (int i = 0; i &lt; sentance.length; i++) {
              for (int j = 0; j &lt; punc.length; j++) {
                  if (sentance[i].equalsIgnoreCase(word + punc[j])) {
                      inputFile.close(); // Problem fixed
                      return "I am a newborn. Not even a year old yet.";
                  }
              }
          }  
      }
  </code></pre>
  
  <p>The question is, when I had set it up the wrong way, Netbeans suggested this:</p>
  
  <pre><code>        File file = new File("src/res/AgeQs.dat");
      try (Scanner inputFile = new Scanner(file)) {
          while (inputFile.hasNext()) {
              String word = inputFile.nextLine();
              for (int i = 0; i &lt; sentance.length; i++) {
                  for (int j = 0; j &lt; punc.length; j++) {
                      if (sentance[i].equalsIgnoreCase(word + punc[j])) {
  
                          return "I am a newborn. Not even a year old yet.";
                      }
                  }
              }  
          }
      }
  </code></pre>
  
  <p>is Netbeans correcting my code, or just removing the closing of the file? Is this a better way to go about doing this? I don't like to use code unless I know exactly what is going on.</p>