简体   繁体   中英

I keep getting NoSuchElement exception in my Java code. What's wrong with my code?

My Java 1.7 program performs various mathematical functions. I have separated the functions into methods and they work ok on their own. I also made a prompt method that asks the users if they want to continue. However, I keep on getting NoSuchElement exception after inputting a number. "Try again? (n/y) " prints, but it doesn't wait for user input and errors automatically. Eclipse Luna highlights this line:

String response = scanner.next();

Here's the main method:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    while (true) {
        System.out.print("Please input a number: ");
        int x = scanner.nextInt();
        System.out.println(chooseOperation(x));
        do_Continue();
        if (do_Continue() == false) {
            break;
        }
    }
    scanner.close();
}

Here's the operation chooser method:

public static int chooseOperation(int n) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Factorial, Fibonacci, or Pisano? ");
    String response = scanner.next();
    scanner.close();
    if (response.equalsIgnoreCase("factorial")) {
        return factorial(n);
    } else if (response.equalsIgnoreCase("fibonacci")) {
        return fibonacci(n);
    } else if (response.equalsIgnoreCase("pisano")) {
        return pisano(n);
    } else {
        System.out.print("Invalid response. ");
        chooseOperation(n);
        return n;
    }
}

And here's the prompt method:

public static boolean do_Continue() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Try again? (n/y): ");
    String response = scanner.next();
    scanner.close();
    if (response.equalsIgnoreCase("n")) {
        return false;
    } else if (response.equalsIgnoreCase("y")){
        return true;
    } else {
        System.out.print("Invalid response. ");
        do_Continue();
    }
    return false;
}

If this helps, here are the methods for the math functions:

public static int factorial(int n) {
    if (n==1 || n==0) {
        return 1;
    } else {
        return n*factorial(n-1);
    }
}

public static int fibonacci(int n) {
    if (n==1 || n==0) {
        return n;
    } else {
        return fibonacci(n-1) + fibonacci(n-2);
    }
}

public static int pisano(int n) {
    if (n==1 || n==0) {
        return n;
    } else {
        return (fibonacci(n-1) + fibonacci(n-2)) % 7;
    }
}

As you can see, except for the main method, all my methods are recursive. Thanks! :)

Do not call

scanner.close();

When you do that, you close() System.in ! Then when you attempt to construct your new Scanner(System.in); it doesn't work (because System.in is closed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM