在try / catch块中使用Scanner的NoSuchElementException

时间:2017-12-13 23:31:54

标签: java input java.util.scanner

对于一个有两种可能游戏模式的简单迷宫游戏,我试图在游戏开始时使用扫描仪来询问玩家的名字。我决定创建一个类MenuParser,我想在其中使用接受对象并将它们传递给各自方法的静态方法。

由于某种原因,Java继续返回相同的错误:

  

线程中的异常" main" java.util.NoSuchElementException at   java.util.Scanner.throwFor(未知来源)at   java.util.Scanner.next(未知来源)at   java.util.Scanner.nextInt(未知来源)at   java.util.Scanner.nextInt(未知来源)at   game.MenuParser.startUpMenu(MenuParser.java:26)at   game.MainProgram.main(MainProgram.java:18)

我已经查看了类似的问题(例如this onethis one),了解如何处理从try / catch块返回值以及导致NoSuchElementException的原因Scanner,但我似乎无法弄明白。我试图移动return,同时简化Scanner(例如,只需删除try / catch块),但都没有解决问题。

非常感谢任何帮助。

主要方法如下:

package game;

import world.Maze;

public class MainProgram {

    public static void main(String[] args) {

        // Initialize objects concerned with playing the game
        Player myPlayer = new Player();
        Maze myMaze = new Maze();

        // Print welcome message and ask the player for his/her name
        System.out.println("Hi there! Welcome to this maze game in Java by MV. \n");
        myPlayer.setPlayerNameWithScanner();

        // Ask the player which game mode he/she would like to play: single player or multiplayer?
        int choice = MenuParser.startUpMenu();
        System.out.println(choice);
        // Parse input and depending on the answer initialize the corresponding menu to start the game
    }

}

MenuParser类如下所示:

package game;

import java.util.InputMismatchException;
import java.util.Scanner;

public class MenuParser {

    public static void pause() {

        System.out.println("Please hit enter to continue.");
        Scanner hitEnter = new Scanner(System.in);
        hitEnter.nextLine();
        hitEnter.close();
    }

    public static int startUpMenu() {

        int choice = 0;

        System.out.println("Which game mode would you like to play?" + "\n");
        System.out.println("\t 1. Single player.");
        System.out.println("\t 2. Robot.");

        try(Scanner startUp = new Scanner(System.in)){

            choice = startUp.nextInt();
            System.out.println(choice);

            if(choice != 1 || choice != 2) {

                System.out.println("Sorry, this is not a valid choice.");
                startUpMenu();
            }
        }
        catch(InputMismatchException im) {

            System.out.println("Sorry, it seems the input is of wrong type.");
            startUpMenu();
        }
        return choice;
    }
}

Player类如下所示:

package game;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Player {

    private String playerName;
    private int playerStepScore;

    public Player () {
    }

    public void setPlayerNameWithScanner(){

        System.out.println("Please enter your name. \n");

        try(Scanner playerNameScanner = new Scanner(System.in);){

            String playerName = playerNameScanner.nextLine();
            setPlayerName(playerName);
        }
        catch(InputMismatchException im) {
            System.out.println("Input seems to be wrong");
            setPlayerNameWithScanner();
        }
    }

    public String getPlayerName() {
        return playerName;
    }
    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }
    public int getPlayerStepScore() {
        return playerStepScore;
    }
    public void setPlayerStepScore(int playerStepScore) {
        this.playerStepScore = playerStepScore;
    }


}

以前的主要方法(使用多个扫描程序,包括try-with-recources,没有任何错误或例外)如下所示:

package game;

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;
import game.Cell;
import game.Maze;
import game.Border;

public class Program {

    public static void main(String[] args) {

        /*
         * General variables that will be used throughout the loops
         * that follow; maxXCoor and maxYCoor are used as maximum values
         * to initialize a grid of a certain size; myMaze is a Maze object
         * which is needed to access its methods throughout the loops.
         */
        int maxXCoor = 0;
        int maxYCoor = 0;
        Maze myMaze = new Maze();

        /*
         * A Scanner is invoked to simply deal with the entry
         * of the file name; this location is then saved in the
         * variable fileName, which is then passed onto the next
         * Scanner and FileReader.
         */
        System.out.println("Please enter a file name");
        Scanner scan = new Scanner(System.in);
        String fileName = scan.nextLine();
        scan.close();
        System.out.println(fileName);

        /*
         * This first block is used to scan the entire input file
         * and look for maxXCoor and maxYCoor to initialize the grid.
         */
        try(Scanner input = new Scanner(new FileReader(fileName));){

            int count = 1;
            while(input.hasNextLine()) {

                /*
                 * Every line is read as a String which is then 
                 * split using a comma as the delimiter. Every
                 * part that is delimited by a comma is then put into an
                 * array called words.
                 */
                String line = input.nextLine();
                String[] words = line.split(",");

                /*
                 * Basic sanity check to make sure the input file uses
                 * the template which was agreed upon, where the first line
                 * are just headers.
                 */
                if(count == 1) {

                    if(!(words[0].toLowerCase()).equals("xcoordinate")) {

                        System.out.println("It looks like there is an issue with the template of your input file.");
                        System.out.println("Please check your input file and try again!");
                        System.exit(0);
                    }
                }
                /*
                 * Once we're not at line one anymore, we can start checking
                 * the current coordinates, xCoor and yCoor, against
                 * the maximum coordinates, maxXCoor and maxYCoor.
                 */
                else {

                    int xCoor = Integer.parseInt(words[0]);
                    int yCoor = Integer.parseInt(words[1]);

                    if(xCoor > maxXCoor) {

                        maxXCoor = xCoor;
                    }
                    if(yCoor > maxYCoor) {

                        maxYCoor = yCoor;
                    }
                }
                count++;
            }
        }
        catch(FileNotFoundException fe) {
            System.out.println("File not found");
            System.exit(0);
        }

        /*
         * As Java uses zero-indexing, we will want to
         * augment the values of our maximum coordinates
         * by one, to avoid IndexOutOfBoundsExceptions in
         * the future. After that we initialize a 2D array
         * of type Cell with these coordinates.
         */
        maxXCoor++;
        maxYCoor++;
        Cell[][] myGrid = new Cell[maxXCoor][maxYCoor];


        try(Scanner input = new Scanner(new FileReader(fileName));){

            int count = 1;
            while(input.hasNextLine()) {

                String line = input.nextLine();
                String[] words = line.split(",");

                if(count > 1) {

                    int xCoor = Integer.parseInt(words[0]);
                    int yCoor = Integer.parseInt(words[1]);
                    Border borderNorth = new Border(words[2]);
                    Border borderSouth = new Border(words[3]);
                    Border borderEast = new Border(words[4]);
                    Border borderWest = new Border(words[5]);
                    Cell myCell = new Cell(borderNorth,borderSouth,borderEast,borderWest);
                    myGrid[xCoor][yCoor] = myCell;

                }
                count++;
            }
        }
        catch(FileNotFoundException fe) {
            System.out.println("File not found");
            System.exit(0);
        }

        for(int x = maxXCoor-1; x >= 0; x--) {
            // Draw north & south wall
            for(int y = 0; y < maxYCoor; y++) {
                Border myBorderNorth = myGrid[x][y].getNorthWall();
                String myTypeNorth = myBorderNorth.getType();
                if(myTypeNorth.equals("wall")) {
                    System.out.print("+---");
                }
                else {
                    System.out.print("+   ");
                }
                if(y == maxYCoor-1) {
                    System.out.println("+");
                }
            }
            // Draw west & east wall
            for(int y = 0; y < maxYCoor; y++) {
                Border myBorderWest = myGrid[x][y].getWestWall();
                String myTypeWest = myBorderWest.getType();
                if(myTypeWest.equals("wall")) {
                    System.out.print("|   ");
                }
                else {
                    System.out.print("    ");
                }
                // Draw last eastern wall
                if(y == maxYCoor-1) {
                    System.out.println("|");                    
                }
            }
            // Draw last southern wall
            if(x == 0) {
                for(int y = 0; y < maxYCoor; y++) {
                    System.out.print("+---");
                }
                System.out.println("+");
            }
        }
        myMaze.setGrid(myGrid);
    }

}

1 个答案:

答案 0 :(得分:3)

您有以下代码:

        try(Scanner startUp = new Scanner(System.in)){
            //stuff
        }

这被称为资源尝试。当此try块执行完毕后,代码将自动关闭Scanner。

但是,关闭与System.in相关联的扫描程序后,System.in将保持关闭状态。如果您尝试阅读其他内容,即使使用新的NoSuchElementException,也会获得Scanner。这似乎是这里发生的事情。最有可能的是,setPlayerNameWithScanner正在以类似的方式关闭扫描仪,导致您的问题。