0

I'm trying to make an if statement that would allow the user to exit the program by typing quit. My problem lies in that another if statement I have in the same block, albeit lower down, catching to ensure that the string entered is no longer than 3 characters long (required for the program) and that if statement takes precedence and cancels out the "quit" if statement. What am I doing wrong that is causing this?

        System.out.print("Guess #"+numGuess+": ");
            guess = input.nextLine();
            guess.toUpperCase();
        if(guess == "quit" || guess == "QUIT"){
            done = true;
            System.out.println("You lose.");
            display();
        }
        if(guess.length() != 3){
            while(guess.length() < 3){
                System.out.println("Guess too short");
                System.out.print("Guess #"+numGuess+": ");
                guess = input.nextLine();
                guess.toUpperCase();
            }
            while(guess.length() > 3){
                System.out.print("Guess too long");
                System.out.print("Guess #"+numGuess+": ");
                guess = input.nextLine();
                guess.toUpperCase();
            }
        }
  • Use `else if(guess.length() != 3){` ... use `else if` to make sure that both the quit and 3 length conditions are not both evaluated. – Tim Biegeleisen Nov 15 '16 at 02:03

4 Answers4

-1

You should be using the String.equals() method instead of the == operator to compare guess to quit. When you compare String using == it tests if they are the same object, not if the text in the string is the same. Your first if statement is never true even if guess does contain the word quit or QUIT.

Govind Parmar
  • 19,325
  • 7
  • 50
  • 80
-1

Keep things simple:

  1. Make your quitting if block return; to exit your program (after disply();)
  2. Don't use == to compare strings. Use .equals() or better .equalsIgnoreCase()
Bohemian
  • 389,931
  • 88
  • 552
  • 692
-1

String comparison if(guess == "quit" || guess == "QUIT") in your code is wrong. If you want to compare the contents of a string you have to use .equals() method. == compares the references not the contents.

Change if(guess == "quit" || guess == "QUIT") to if(guess.equalsIgnoreCase("QUIT"))

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
v3nM
  • 912
  • 1
  • 12
  • 19
  • Who ever down-voted an answer should justify a reason behind it. If the answer is wrong, then a comment along with that is a very reasonable and acceptable action. I simply don't like my answer down-voted just because the the question is a duplicate. That does not serve the purpose of down-voting. Very misleading. Whoever did this should atleast justify – v3nM Nov 16 '16 at 16:49
-1

Try simplifying the code a bit.

 while (guess.length() != 3) {
    System.out.print("Guess #"+numGuess +": ");
    guess = input.nextLine();
    guess = guess.toUpperCase();

    if(guess.equalsIgnoreCase("quit")){
        done = true;
        System.out.println("You lose.");
        display();
        break;
    }

    if (guess.length() < 3){
        System.out.println("Guess too short");
    }
    else if (guess.length() > 3){
        System.out.print("Guess too long");
    }
   numGuess++;
} 
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216