SOLVED: The issue was that I had another scanner set to System.in earlier in my code, thank you to @Abra for mentioning that it could be the cause of the issue!
while (Objects.equals(cycle, "yes")) {
try (Scanner input = new Scanner(System.in)) {
System.out.println("\nWould you like to gather more information? (y/n):");
String answer = input.nextLine();
if (Objects.equals(answer, "y")) {
menu();
} else if (Objects.equals(answer, "n")) {
System.out.println("Have a nice day!");
cycle = "no";
} else {
System.out.println(
"Sorry the respone entered can not be read. Please try again using either the letter 'y' or 'n'.");
}
}
}
The above code is outputting the following error no matter what I change:
Would you like to gather more information? (y/n):
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Graph.main(Graph.java:246)
I can't figure out why this is happening my scanner works in other parts of the same code however this one specific area won't work. Any help is appreciated.
EDITED CODE:
while (Objects.equals(cycle, "yes")) {
System.out.println("\nWould you like to gather more information? (y/n):");
String answer = input.nextLine();
if (Objects.equals(answer, "y")) {
menu();
} else if (Objects.equals(answer, "n")) {
System.out.println("Have a nice day!");
cycle = "no";
} else {
System.out.println(
"Sorry, the response entered can not be read. Please try again using either the letter 'y' or 'n'.");
}
}
The scanner is now at the top of this file not near this specific area of code, it reads:
static Scanner input = new Scanner(System.in);