java - try/catch block 中带有 Scanner 的 NoSuchElementException

标签 java input java.util.scanner

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

出于某种原因,Java 不断返回相同的错误:

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at game.MenuParser.startUpMenu(MenuParser.java:26) at game.MainProgram.main(MainProgram.java:18)

我研究过类似的问题(例如 this onethis one ),了解如何处理从 try/catch block 返回值以及导致 NoSuchElementException 的原因扫描仪,但我似乎无法弄清楚。我尝试移动我的 return 并简化 Scanner(例如,通过简单地删除 try/catch block ),但都没有解决问题。

任何帮助将不胜感激。

主要方法如下:

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;
    }


}

之前的 main 方法(使用了多个扫描器,包括 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);
    }

}

最佳答案

您有以下代码:

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

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

但是一旦您关闭与System.in 关联的扫描程序,System.in 就会保持关闭状态。如果您尝试读取其他任何内容,即使您使用新的 Scanner ,您也会收到 NoSuchElementException 。这似乎就是这里正在发生的事情。最有可能的是,setPlayerNameWithScanner 正在以类似的方式关闭扫描仪,从而导致您出现问题。

关于java - try/catch block 中带有 Scanner 的 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47803693/

相关文章:

java - 如何以编程方式将异步消费者订阅到 Spring AMQP 中的队列?

java - 当我运行代码时,菜单显示为垂直而不是水平,我该如何更改它?

Java : The constructor JSONTokener(InputStreamReader) is undefined

java - 通过单击按钮删除 Vaadin 表格项目

javascript - 尝试将表单输入保存到变量中

python - 在 Python 中指定 input() 类型?

wordpress - 禁用输入字段上的文本联系表单7 WordPress

java - 不同转义字符的扫描仪困难

java - 如何在java中找到五个最接近零的输入数字

java - 为什么 input.next() 可以用于搜索 ArrayList,而 .nextLine() 则不能?