0

New to the forum. Tried searching but could not find anything that helped. I ran this code on another IDE with java 1.7 without issue. I am now using Eclipse and updated java to 1.8 and am getting the error: java.util.NoSuchElementException regarding line "response = input.nextLine();". Strange thing is that I have another function that returns a string and is coded with the exact same loop (do/while with a pattern check) and it seems to be working fine.

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;
    }
}
Squanchy
  • 1
  • 1
  • Try this [answer](http://stackoverflow.com/questions/36723447/java-util-scanner-throws-nosuchelementexception-when-application-is-started-with) – Fady Saad Apr 26 '17 at 02:46
  • Hey mate just ran your code and it works fine for me check the main method and how your referencing search_again() – HelloWorld Apr 26 '17 at 02:47
  • Thanks! I did some testing and found: http://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input – Squanchy Apr 26 '17 at 04:20
  • Looks like you can't re-initiate a scanner with the same input. I made the scanner global and it seems to be working again. – Squanchy Apr 26 '17 at 04:21

1 Answers1

0

Looks like you can't re-initiate a scanner with the same input. I made the scanner global and it seems to be working again

Squanchy
  • 1
  • 1