-2

I am working on a java program. Right now everything is totally working, and all my functionality is there. However, the part I am stuck on is how to exit out of the program in a do-while loop. I must be getting the syntax wrong.

Basically, I set a switch done which reacts to a user's input. Right now, it's working and loops through the program, but it does not exit if I say "no" to continuing.

Here is the part of the code this is happening:

public void main() {
    String userInput;
    boolean done = true;
    Scanner keyboard = new Scanner(System.in);
    do {
        System.out.println("Welcome to Hangman!");
        System.out.println("Do you want to play?");
        userInput = keyboard.next();
        if (userInput.equals("Yes") || userInput.equals("yes") || userInput.equals("y") || userInput.equals("Y")) {
            done = false;
        } else if (userInput.equals("n") || userInput.equals("no") || userInput.equals("NO") || userInput.equals("No")) {
            done = true;
        }
        while (!done) {
          System.out.println(getDisguisedWord());   
          System.out.println("Guess a letter: ");
          String guess = keyboard.next();
          makeGuess(guess);
          if (gameOver()) {
              String ui;
              System.out.println("Do you want to play again?");
              ui = keyboard.next();
              if (ui.equals("Yes") || ui.equals("yes") || ui.equals("y") || ui.equals("Y")) {
                  done = false;
              } else {
                  done = true;
              }
          }
        }
    } while(done);
}

any tips on how I could handle this better?

James N
  • 523
  • 1
  • 8
  • 30

3 Answers3

1

Your problem isn't what you think it is. To compare Strings, you need to use their built-in equals() method: ui.equals("Y"). Using == to compare them will always return false. For more information, see How do I compare strings in Java?.

Also, you need to flip your done = true and done = false statements (if the user says yes to playing again, they aren't done yet).

Finally, I would recommend changing your keyboard.next() calls to keyboard.nextLine() calls, or else you may run into weird issues, especially if the user enters input that includes whitespace.

EDIT: I noticed some more issues. You're while loop should be while(!done) instead of while(done). Also, I would get rid of your do-while loop, because the while loop is already allowing the user to play as many times as they want, so it is unnecessary.

Community
  • 1
  • 1
mapeters
  • 1,067
  • 7
  • 11
  • thanks so much for the answer! I updated it based on your suggestions. However, this still does not let me exit, and keeps looping back up to the `System.out.println("Welcome to Hangman!");` line inside the beginning of the `do` block. any tips on how to exit the loop and program? – James N Oct 27 '16 at 04:34
  • @JamesN did you flip `done = true` and `done = false`? I notice in your question, you changed the `==` to `.equals()` but didn't fix those. – mapeters Oct 27 '16 at 04:38
  • yes I did. I readjusted the edit. I still have the same problem – James N Oct 27 '16 at 04:42
  • @JamesN see the EDIT at the bottom of my answer. – mapeters Oct 27 '16 at 04:43
0
            boolean flag = true;
            Scanner sc = new Scanner(System.in);
            do{
                System.out.println("***********************************************************");
                System.out.println("Welcome to the School Admissions App !!!   Press X for exit");
                System.out.println("***********************************************************");
                System.out.println("Enter the Student Name: ");
                String student_name=sc.next();
                System.out.println("press y to use this application again. press x to exit from this application ");
                String input_user=sc.next();
                if(input_user.equalsIgnoreCase("y")){
                    flag=true;
                }else{
                    flag=false;
                    System.out.println("Thanks for using it.");
                    }
            }while(flag);
Maninder
  • 1,539
  • 1
  • 10
  • 12
-1

U should use equals method for comparing string value in if statment. ui.equals("yes").

Manmohan
  • 410
  • 4
  • 13