2013-03-13 55 views
5

每當我運行這個時,chooseCave()函數可以正常工作,in.nextInt()。當我選擇山洞,消息彈出在2秒的時間間隔,然後只要它得到過去的那一部分,它給我的錯誤:線程「main」中的異常java.util.NoSuchElementException

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(),當我在main方法中使用while hasNextLine(),我得到了更多的錯誤。當我在chooseCave()方法中使用while hasNextInt()時,它不接受我的輸入。

當我在chooseCave()方法使用if hasNextInt(),它不接受我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(); 

} 

} 

回答

8

您關閉第二Scanner封閉底層InputStream,因此第一Scanner不再能夠從同一InputStreamNoSuchElementException結果讀取。

解決方案:對於控制檯應用程序,請使用單個Scanner來讀取System.in

除外:如前所述,請注意,Scanner#nextInt不會消耗換行符。在嘗試使用Scanner#newLine()再次致電nextLine之前,請確保這些消耗完畢。

參見:Do not create multiple buffered wrappers on a single InputStream

+0

+1,我忘了提及一個輸入問題的多個掃描儀。 – syb0rg 2013-03-14 00:37:16

+0

感謝你和syb0rg。我只需要使用掃描儀刪除方法,並在主要方法中放置等效物。有用!在新的編程語言中取得第一次成功......感覺非常好。 :) – 2013-03-15 08:49:07

5

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或單獨的單詞。


此外,請確保您,您只使用輸入一個終端只能使用一個Scanner。這可能是這個例外的另一個原因。


末注:與.equals()功能,而不是==操作比較一個String

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

謝謝!我希望在這裏有足夠的觀點來支持你。我解決了你所說的問題,但仍然無效......但好消息是,一旦我將掃描儀從方法中取出,我就可以將其全部置於主要方法中,並且工作正常。數字初始化也幫助。再次感謝! :) – 2013-03-14 16:01:41

1

Reimeus是正確的,你看因爲這個in.closechooseCave()。 另外,這是錯誤的。

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

您應該使用equals而不是「==」。

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

同意,但不是錯誤的原因。但後來可能會導致問題... – syb0rg 2013-03-14 00:37:46

0

根本不關閉

從代碼中刪除in.close()

+0

這複製了接受的答案,但質量低得多。 – JasonMArcher 2014-10-30 18:23:39

+0

我也面臨同樣的問題。在互聯網上搜索了很多東西后,最後從我的代碼中刪除in.close()爲我工作。這就是爲什麼我回答了這個問題。 – 2014-10-30 18:29:09

+0

這個答案真的已經給出,並有一個更好的解釋。 – 2014-11-01 04:11:29

-1

我得到了同樣的錯誤,並沒有這些解決方案的工作。嘗試檢查您的PC上安裝的Java版本。

它在Java 1.8上適用於我。

相關問題