1

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

Stitch
  • 31
  • 6
  • 1
    Based on `letter.equals("A")` you seem to know how to properly compare strings. So why are you using `}while (ans == "y" || ans == "y");`? – Pshemo Oct 22 '17 at 10:28
  • OH. I didnt catch that! Thank you for pointing that out! It's 6:30 A.M. here and I've been working on this all night. My brain must be tired and thinking of C++. – Stitch Oct 22 '17 at 10:32
  • Anyway something tells me in your `guessingGame();` or `calcDecide();` you are creating another Scanner which will read from System.in which then you are closing. Closing scanner will also close its resource from which it is reading (here System.in) which will prevent from using it farther. If that is the case then solution is to not creating many Scanners for same resource (if resource should stay opened). So either make that scanner a class field, or let those method accept it as parameter. – Pshemo Oct 22 '17 at 10:36
  • Thanks for catching the real issue. In your original comment I had changed it and was wondering why it was still coming up with the same error message! – Stitch Oct 22 '17 at 10:42
  • Was that the real cause of the problem or is exception still getting thrown? – Pshemo Oct 22 '17 at 10:45
  • Well, to be honest I'm trying to figure out if I should make the scanner a class field (This is my midterm and I heard doing that is bad practice?) So was asking my instructor first, and was going to wait for his feedback. Meanwhile, I'm trying to mess with the scanner going inside the parameters, but I'm having trouble calling it in my main. It keeps giving me error messages saying that its not a local variable, or that it's name needs to be changed to a string? Not sure, I'm going to have to mess around with it more before I can give a solid answer – Stitch Oct 22 '17 at 10:54

0 Answers0