0

For a project I have to make to hangman game. Everything works but I'm trying make a loop which lets the user play again if he/she chooses.


import java.util.Scanner;
//this is the main method where I'm trying to add the loop//
public class hangMan {
    public static void main(String[] args) {
        String repeat = "y";
        while (repeat == "y") {
            Scanner sc = new Scanner(System.in);
            String w = randomWord("cat", "kite", "touch", "yellow", "abdomen");
            if (w.equals("cat")) {
                threeword();
            }
            if (w.equals("kite")) {
                fourword();
            }
            if (w.equals("touch")) {
                fiveword();
            }
            if (w.equals("yellow")) {
                sixword();
            }
            if (w.equals("abdomen")) {
                sevenword();
            }
            System.out.println("Do you want to play again? (y/n): ");
            repeat = sc.next();
            sc.close();
        }
    }

I was expecting for the loop to keep repeating the game if the user entered "y" for repeat and for the game to stop if the user entered any other character. However, instead I keep getting a java.util.NoSuchElementException error. I would appreciate if anyone can help me create a loop and explain to me why what I did to mess up. Thank you

  • 1
    Does this answer your question? [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Tom Nov 03 '19 at 04:39
  • 1
    The last line of your loop closes `sc`, that also closes `System.in` and that is a global (and thus every further call to read will fail). Also, don't use `==` in `while (repeat == "y")` that will only work for the first iteration. – Elliott Frisch Nov 03 '19 at 04:42
  • In addition to @ElliottFrisch comment, the `==` operator evaluates primitives by value and objects by reference, use `equals()` method to compare/evaluate objects. – itwasntme Nov 03 '19 at 05:32

2 Answers2

0
  • Declare your Scanner object outside of the while loop
  • Write sc.close outside the while block i.e before the end of main curly braces
  • Don't use == , use equals() function. Note the difference between == and equals() function for String.
0

The complete code is:

public static void main(String[] args) {
        String repeat = "y";
        Scanner sc = new Scanner(System.in); //Fix: Only one scanner object will do
        while (repeat.equals("y")) {
            String w = randomWord("cat", "kite", "touch", "yellow", "abdomen");
            if (w.equals("cat")) {
                threeword();
            }
            if (w.equals("kite")) {
                fourword();
            }
            if (w.equals("touch")) {
                fiveword();
            }
            if (w.equals("yellow")) {
                sixword();
            }
            if (w.equals("abdomen")) {
                sevenword();
            }

            System.out.println("Do you want to play again? (y/n): ");
            repeat = sc.next();

            /*Fix: Close the scanner object only when user doesn't want to continue */
            if(!repeat.equals("y")){
                sc.close();
            }
        }
    }
Saswata
  • 1,290
  • 2
  • 14
  • 28