我收到了NoSuchElementException

时间:2015-02-26 09:34:30

标签: java 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退出时它都会停止工作。 (当我输入&#39; q&#39;或Q&#39;以及我选择1,2或3之后) 我假设它是关于扫描仪的东西,但我真的不知道如何解决它。

1 个答案:

答案 0 :(得分:0)

您正在同一个流上创建两个扫描程序,并在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();
    }

}