线程“主”中的异常java.util.NoSuchElementException [英] Exception in thread "main" java.util.NoSuchElementException

查看:979
本文介绍了线程“主”中的异常java.util.NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行它, chooseCave()函数可以正常工作在 in.nextInt()中。当我选择洞穴时,消息以2秒的间隔弹出,然后一旦超过该部分,它会给我错误:

 线程中的异常mainjava.util.NoSuchElementException:找不到行
在java.util.Scanner.nextLine(Unknown Source)
在Dragon.main(Dragon.java :81)

我尝试过 hasNextLine() hasNextInt(),而当我使用而在 main中的hasNextLine() / code>方法,我得到更多的错误。当我在 chooseCave()方法中使用而hasNextInt()时,它不接受我的输入。 p>

当我使用如果$ code中有hasNextInt() chooseCave()方法,它不接受我对 playAgain 字符串的输入,并直接进入另一个游戏,但是然后$ code> hasNextInt() boolean返回 false ,并无限地发送哪个洞...。



经历了错误报告和Java文档和Stack Overflow的类似问题。请帮助。

  import java.util.Scanner; 
public class Dragon {

public static void displayIntro(){
System.out.println(你在一个充满了龙的土地,在你面前) ;
System.out.println(你看到两个洞穴,一个洞,龙是友好的);
System.out.println(并将与你分享他的宝藏,另一只龙);
System.out.println(贪婪饥饿,会在你面前吃你);
System.out.println('');
}

public static int chooseCave(){
扫描仪in = new Scanner(System.in);
int cave = 0;
while(cave!= 1&& cave!= 2){
System.out.println(你会进入哪个洞?(1或2));

cave = in.nextInt();

}
in.close();
返回洞穴;
}

public static void checkCave(int selectedCave){
System.out.println(你接近洞穴...);
try
{
//睡眠至少n毫秒。
// 1毫秒= 1/1000秒。
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(唤醒过早);
}
System.out.println(It is dark and spooky ...);
try
{
//睡眠至少n毫秒。
// 1毫秒= 1/1000秒。
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(唤醒过早);
}
System.out.println(大龙在你前面跳出来,他打开他的下巴和...);
try
{
//睡眠至少n毫秒。
// 1毫秒= 1/1000秒。
Thread.sleep(2000);
}
catch(InterruptedException e)
{
System.out.println(唤醒过早);
}

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

如果(selectedCave == friendlyCave){
System.out.println(给你宝藏!);
}
else {
System.out.println(Gobbles you down in one bite!);
}



}
public static void main(String [] args){
扫描仪内部=新的扫描仪(System.in );
String playAgain =yes;
boolean play = true;
while(play){
displayIntro();
int caveNumber = chooseCave();
checkCave(caveNumber);
System.out.println(你想再玩吗?(是或否));
playAgain = inner.nextLine();
if(playAgain ==yes){
play = true;
}
else {
play = false;
}
}
inner.close();

}

}


解决方案

您关闭第二个 Scanner ,它关闭底层的 InputStream ,因此第一个 Scanner 不能从相同的 InputStream NoSuchElementException 结果中读取。



解决方案:对于控制台应用程序,使用单个 Scanner System读取。在中。



Aside:如上所述,请注意 Scanner#nextInt 不使用换行符。确保在尝试使用 Scanner#newLine()之前再次调用 nextLine b
$ b

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


Whenever I run this, the chooseCave() function works fine with the in.nextInt(). When I choose the cave, the messages pop up at 2-second intervals, and then as soon as it gets past that part, it gives me the error:

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

I have tried hasNextLine() and hasNextInt(), and when I use while hasNextLine() in the main method, I get a ton more errors. When I use while hasNextInt() in the chooseCave() method, it doesn't accept my input.

When I use if hasNextInt() in the chooseCave() method, it doesn't accept my input for the playAgain string, and goes straight into another game, but then the hasNextInt() boolean returns false and it spams "Which cave..." infinitely.

I've gone through the error reports and the Java-docs and Stack Overflow's with similar problems. Please help.

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();

}

}

解决方案

You close the second Scanner which closes the underlying InputStream, therefore the first Scanner can no longer read from the same InputStream and a NoSuchElementException results.

The solution: For console apps, use a single Scanner to read from System.in.

Aside: As stated already, be aware that Scanner#nextInt does not consume newline characters. Ensure that these are consumed before attempting to call nextLine again by using Scanner#newLine().

See: Do not create multiple buffered wrappers on a single InputStream

这篇关于线程“主”中的异常java.util.NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆