I'm pretty new to coding, and I understand how to do a do while for a yes or no answer in C++. But java is giving me a hard time. This code doesn't give me any errors until I run it. When it asks for the yes or no answer it will say:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at midterm.main(midterm.java:40)
Line 40 is the:
ans = s.next();
Maybe I'm doing something wrong with String, I've tried char but had no luck either. Here is my code:
public class midterm
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String ans;
do
{
System.out.println("Welcome to the Menu!\nA)Guessing Game\nB)Calculator\n"
+ "\nEnter Letter: ");
String letter = s.next();
s.nextLine();
if ((letter.equals("A")) || (letter.equals("a")))
{
guessingGame();
}
else
{
calcDecide();
}
System.out.println("\nWould you like to go back to the menu or exit?\n"
+ "Press 'Y' yes or 'E' for exit: ");
ans = s.next();
s.nextLine();
}while (ans == "y" || ans == "y");
s.close();
}
Not sure why this was marked as a duplicate. I feel that people don't read the entire thing and like to assume this. This WAS NOT a simple String mess-up(That's part of the mistake,) but there's a bigger issue here, which I believe to be the scanner