title: Why does PMD signal me that he scanner isn't closed?
body: <p>So in the following code PMD tells me the that the Scanners keyboard and scan are unclosed with a [CLoseResource] warning. I think I close I made sure that it is always closed, so I don't know what is causing this warning. The code itself works fine, and those are the only serious warnings PMD is throwing at me.</p>
  <p>Codebit 1:</p>
  <pre><code>public static void main(final String... args) {
          Scanner scan = null;
          try {
              if (args[0].equals(&quot;-f&quot;)) {
                  try {
                      scan = new Scanner(new File(args[1]));
                  }catch(FileNotFoundException e) {
                      System.out.println(e.getMessage());
                      System.exit(START_ERROR);
                  }
              }else {
                  System.out.println(&quot;Unbekanntes Kommando.&quot;);
                  System.exit(START_ERROR);
              }
          } catch (ArrayIndexOutOfBoundsException e) {
              System.out.println(&quot;Nichts eingegeben.&quot;);
              System.exit(START_ERROR);
          }
          try {
              validateSize(scan);
          }catch (InvalidBoardLayoutException e) {
              System.out.println(e.getMessage());
              scan.close();
              System.exit(BOARD_ERROR);
          }
          scan.close();
          try {
              scan=new Scanner(new File(args[1]));
          } catch (FileNotFoundException e) { //This should be impossible
              System.out.println(&quot;This should be impossible lmao&quot;);
              scan.close();
              System.exit(666);
          }
          try {
              validateFields(scan);
          }catch(InvalidFieldException e) {
              System.out.println(e.getMessage());
              scan.close();
              System.exit(BOARD_ERROR);
          }
          scan.close();
          try {
              scan=new Scanner(new File(args[1]));
          } catch (FileNotFoundException e) { //This should be impossible
              System.out.println(&quot;This should be impossible lmao&quot;);
          }
          final Game game = new Game(scan);
          game.play();
          scan.close();
      }
  </code></pre>
  <p>Codebit 2:</p>
  <pre><code>        System.out.println(&quot;Enter your next move!&quot;);
          Scanner keyboard = new Scanner(System.in);
          String move = keyboard.nextLine();
          if (move.isEmpty()) {
              won = true;
              keyboard.close();
              return;
          }
          if (!validateFormat(move)) {
              System.out.println(&quot;Invalid format, try again.&quot;);
              return;
          }
          String[] moveAr;
          try {
              moveAr = move.split(&quot;,&quot;);
          } catch (PatternSyntaxException e) { //this should be impossible
              System.out.println(e.getMessage());
              return;
          }
          try{
              validFields(moveAr);
          }catch(InvalidTurnException e){
              System.out.println(e.getMessage());
              return;
          }
          final char colour = getColour(moveAr[0]);
          for (final String s : moveAr) {
              final int line = Character.getNumericValue(s.charAt(1)) - 1;
              final int column = getColumn(s.charAt(0));
              this.spielFeld[line][column] = Character.toUpperCase(this.spielFeld[line][column]);
              final char columnc = s.charAt(0);
              if (columnCrossed(columnc)){
                  points += crossedValues(columnc);
              }
          }
          if (colourComplete(colour)){
              points += 5;
              coloursCrossed++;
          }
          if (coloursCrossed &gt;= 2){
              won = true;
              keyboard.close();
          }
          System.out.println(&quot;Momentane Punkte: &quot;+points);
      }```
  </code></pre>