我收到 NoSuchElementException

I'm getting NoSuchElementException

这是我收到的错误消息:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at harvestMoon.Program.menu1(Program.java:38)
at harvestMoon.Program.<init>(Program.java:18)
at harvestMoon.Driver.main(Driver.java:8)

这是我的代码:

package harvestMoon;

import java.util.Scanner;
import java.util.concurrent.Delayed;

public class Program {
    // Test!
        // Program class not yet done..
    private Field f;
    private Avatar a;

    public Program(){

        a= new Avatar();
        f= new Field(a);
        f.displayField();

        menu1();
    }
    //incomplete
    public void swapItemMenu(){
        System.out.println("What equipment would you like to be active?");
        System.out.println("1 - Hoe, 2 - Watering Can, 3 - Sickle");
        Scanner sc1=new Scanner(System.in);
        int inp=sc1.nextInt();
        a.swapTool(inp);

        sc1.close();
    }

    public void menu1(){
        char input;
        Scanner sc= new Scanner(System.in);


        do
        {
            input = sc.next().charAt(0);
            //movement
            if(input=='A'||input=='a'||input=='W'||input=='w'||input=='S'||input=='s'||
                    input=='D'||input=='d')
            a.moveMe(input);
            //swaptool
            if(input=='Q'||input=='q')
                swapItemMenu();
            //usetool
            if(input=='E'||input=='e')
                f.updateField();
            //examine tile
            if(input=='F'||input=='f'){
                f.examineTile();
                //delays for 2 seconds
                try {
                    Thread.sleep(2000);                 //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }

            }
            f.displayField();
        }while(input!='x');
        sc.close();

    }


}

每次我退出 swapItemMenu 时它都会停止工作。 (当我输入 'q' 或 'Q' 并从 1、2 或 3 中选择后) 我假设这是关于扫描仪的问题,但我真的不知道如何修复它。

您正在同一流上创建两个扫描器并在 swapItemMenu 中关闭其中一个。尝试只使用一台扫描仪:

public class Program {

    private final Scanner sc = new Scanner(System.in); // scanner extracted to class member

    private Field f;
    private Avatar a;

    public Program() {
        a = new Avatar();
        f = new Field(a);
        f.displayField();

        menu1();
    }

    public void swapItemMenu() {
        System.out.println("What equipment would you like to be active?");
        System.out.println("1 - Hoe, 2 - Watering Can, 3 - Sickle");
        int inp = sc.nextInt(); // use existing scanner
        a.swapTool(inp);

        // no closing
    }

    public void menu1() {
        char input;

        // scanner instantiated when the object is created

        do {
            input = sc.next().charAt(0);
            if (input == 'A' || input == 'a' || input == 'W' || input == 'w' || input == 'S' || input == 's' ||
                    input == 'D' || input == 'd')
                a.moveMe(input);
            if (input == 'Q' || input == 'q')
                swapItemMenu();
            if (input == 'E' || input == 'e')
                f.updateField();
            if (input == 'F' || input == 'f') {
                f.examineTile();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
            }
            f.displayField();
        } while (input != 'x');
        sc.close();
    }

}