由于NoSuchElementException,无法提示用户输入

时间:2015-04-16 20:14:53

标签: java exception java.util.scanner do-while

我创建了这个测试类来测试一个循环的方法,直到用户输入正确的是/否答案。它告诉我这个NoSuchElementFound Exception,它说没有新线。我该如何解决?

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Test.isDangerousMethod(Test.java:15)
at Test.main(Test.java:4)



import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        System.out.println(isDangerousMethod());
    }
    private static boolean isDangerousMethod() {
        String answer;
        Scanner in = new Scanner(System.in);
        do {
            System.out.print("Warning: The command you entered may alter ");
            System.out.print("the files in your working directory. ");
            System.out.print("Uncommitted changes may be lost. ");
            System.out.println("Are you sure you want to continue? (yes/no)");
            answer = in.nextLine();
        } while (!answer.equals("yes") && !answer.equals("no"));
        in.close();
        return !answer.equals("no");
    }
}

0 个答案:

没有答案