0
新的論壇。嘗試搜索,但找不到任何幫助。我用java 1.7在另一個IDE上運行這段代碼沒有問題。我現在使用Eclipse和更新的Java到1.8,並得到錯誤:關於行「response = input.nextLine();」的java.util.NoSuchElementException。奇怪的是,我有另一個函數返回一個字符串,並用完全相同的循環編碼(做/檢查模式),它似乎工作正常。java做while循環與控制檯輸入
static boolean search_again() {
String response;
//Scanner input = new Scanner(System.in);
do {
System.out.println("Would you like to search another site? ");
Scanner input = new Scanner(System.in);
response = input.nextLine();
input.reset();
}
while(!response.toUpperCase().matches("Y|YE|YES") && !response.toUpperCase().matches("N|NO"));
if (response.matches("[nN[oO]?]")) {
System.out.println("Goodbye");
return false;
}
else {
System.out.println("OK - search again!");
return true;
}
}
試試這個[答案](http://stackoverflow.com/questions/36723447/java-util-scanner-throws-nosuchelementexception-when-application-is-started與) –
嘿隊友剛剛運行您的代碼,它適用於我檢查主要方法,以及如何引用search_again() – HelloWorld
謝謝!我做了一些測試,發現:http://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input – Squanchy