为什么pmd给我发信号说他没关扫描仪?

6tqwzwtp  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(372)

因此,在下面的代码中,pmd告诉我扫描仪键盘和扫描未关闭,并发出[closeresource]警告。我想我关闭了我确保它总是关闭的,所以我不知道是什么导致了这个警告。代码本身运行良好,这是pmd向我发出的唯一严重警告。
代码位1:

public static void main(final String... args) {
        Scanner scan = null;
        try {
            if (args[0].equals("-f")) {
                try {
                    scan = new Scanner(new File(args[1]));
                }catch(FileNotFoundException e) {
                    System.out.println(e.getMessage());
                    System.exit(START_ERROR);
                }
            }else {
                System.out.println("Unbekanntes Kommando.");
                System.exit(START_ERROR);
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Nichts eingegeben.");
            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("This should be impossible lmao");
            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("This should be impossible lmao");
        }
        final Game game = new Game(scan);
        game.play();
        scan.close();
    }

代码位2:

System.out.println("Enter your next move!");
        Scanner keyboard = new Scanner(System.in);
        String move = keyboard.nextLine();
        if (move.isEmpty()) {
            won = true;
            keyboard.close();
            return;
        }
        if (!validateFormat(move)) {
            System.out.println("Invalid format, try again.");
            return;
        }
        String[] moveAr;
        try {
            moveAr = move.split(",");
        } 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 >= 2){
            won = true;
            keyboard.close();
        }
        System.out.println("Momentane Punkte: "+points);
    }```
9wbgstp7

9wbgstp71#

如果try中的代码抛出未捕获的异常,则扫描程序不会关闭。正如评论中已经提到的,try with resources是处理这个问题的正确方法。

相关问题