异常线程“main” java.util.NoSuchElementException(Except

2019-07-21 13:55发布

每当我跑这一点, chooseCave()函数正常工作与in.nextInt() 当我选择山洞,消息弹出在2秒的时间间隔,然后只要它得到过去的那一部分,它给我的错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Dragon.main(Dragon.java:81)

我曾尝试hasNextLine()hasNextInt()当我使用while hasNextLine()main方法,我得到一吨多的错误。 当我使用while hasNextInt()chooseCave()方法,它不接受我的输入。

当我使用if hasNextInt()chooseCave()方法,它不接受我输入playAgain串,并直接进入另一场比赛,但随后hasNextInt()则返回false ,它修建垃圾“哪个山洞.. 。“ 无限地。

我已经通过错误报告和Java的文档和堆栈溢出的类似的问题了。 请帮忙。

import java.util.Scanner;
public class Dragon {

public static void displayIntro() {
    System.out.println("You are in a land full of dragons. In front of you, ");
    System.out.println("You see two caves. In one cave, the dragon is friendly");
    System.out.println("and will share his treasure with you. The other dragon");
    System.out.println("is greedy and hungry, and will eat you on sight");
    System.out.println(' ');
}

public static int chooseCave() {
    Scanner in = new Scanner(System.in);
    int cave = 0;
    while (cave != 1 && cave != 2) {
        System.out.println("Which cave will you go into? (1 or 2)");

        cave = in.nextInt();

    }
    in.close();
    return cave;
} 

public static void checkCave(int chosenCave) {
    System.out.println("You approach the cave...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }
    System.out.println("It is dark and spooky...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }
    System.out.println("A large dragon jumps out in front of you! He opens his jaws and...");
    try
       {
       // Sleep at least n milliseconds.
       // 1 millisecond = 1/1000 of a second.
       Thread.sleep( 2000 );
       }
    catch ( InterruptedException e )
       {
       System.out.println( "awakened prematurely" );
       }

    double friendlyCave = Math.ceil(Math.random() * 2);

    if (chosenCave == friendlyCave) {
        System.out.println("Gives you his treasure!");
    }
    else {
        System.out.println("Gobbles you down in one bite!");
    }



}
public static void main(String[] args) {
    Scanner inner = new Scanner(System.in);
    String playAgain = "yes";
    boolean play = true;
    while (play) {
        displayIntro();
        int caveNumber = chooseCave();
        checkCave(caveNumber);
        System.out.println("Do you want to play again? (yes or no)");
        playAgain = inner.nextLine();
        if (playAgain == "yes") {
            play = true;
        }
        else {
            play = false;
        }
    }
    inner.close();

}

}

Answer 1:

您关闭第二Scanner ,其关闭底层InputStream ,因此第一Scanner不能再从同一读取InputStreamNoSuchElementException结果。

解决办法:对于控制台应用程序,使用一个Scanner从阅读System.in

旁白:如前所述,要知道, Scanner#nextInt不消耗换行符。 确保这些尝试呼叫之前消耗nextLine通过再次使用Scanner#newLine()

请参阅: 不要在单一的InputStream创建多个缓冲包装



Answer 2:

nextInt()方法离开\n (端线)符号,并且通过立即拾取nextLine()跳过下一个输入。 你想要做的是使用nextLine()的一切,后来又对其进行分析:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

这是迄今为止,以避免问题的最简单的方法 - 不要混合你的“下一个”的方法。 仅使用nextLine()然后解析int S或单独的词之后。


此外,请确保您只使用一个Scanner ,如果你只使用输入一个终端。 这可能是另一个原因除外。


最后要注意的:一个比较String.equals()函数,而不是==操作符。

if (playAgain == "yes"); // Causes problems
if (playAgain.equals("yes")); // Works every time


Answer 3:

Reimeus是正确的,你在你的chooseCave看到,因为in.close的这个()。 此外,这是错误的。

if (playAgain == "yes") {
      play = true;
}

您应该使用等于,而不是“==”。

if (playAgain.equals("yes")) {
      play = true;
}


Answer 4:

根本就没有关闭

除去in.close()从你的代码。



文章来源: Exception in thread “main” java.util.NoSuchElementException