Joshua Sears
Joshua Sears

Reputation: 17

While loop just not working

I am just wanting some explanation (why) on why the following code is not working and a solution(How) to make the code run.

The goal of the program is to get a user input of a playing card, either J,Q,K,A, lower case or upper case. The program must only accept one of these values so it needs to validate the user input, displaying when its wrong and prompting till an accepted value is entered. The program then must take that user entered value and print the name of the playing card onto the console, Jack, Queen, King, Ace.

package practical_1;

import static java.lang.System.out;
import java.util.Scanner;

public class Question_4 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

            String Usr_Card;                
            out.print("Enter Card Letter Here....   ");
            Usr_Card = input.next();
            Usr_Card = Usr_Card.toUpperCase();


            while (Usr_Card != "K" || Usr_Card != "Q" || Usr_Card != "J" || Usr_Card != "A"){
                out.print("Invalid Input\nEnter Valid Card Letter Here:   ");
                Usr_Card = input.next();
                Usr_Card = Usr_Card.toUpperCase();
            }

            switch (Usr_Card) {
                case ("J"): 
                    out.print("Jack");
                    break;
                case ("Q"): 
                    out.print("Queen");
                    break;
                case ("K"): 
                    out.print("King");
                    break;
                case ("A"): 
                    out.print("Ace");
                    break;
                }

    }

}

Upvotes: 0

Views: 117

Answers (3)

Spidy
Spidy

Reputation: 39992

The OR operator || returns true if any section is true. In your example, if card is not K or not Q or not J or not A, continue the loop.

Let's break that down. User enters a "K"

Usr_Card != "K" // false
Usr_Card != "Q" // true
Usr_Card != "J" // true
Usr_Card != "A" // true

false || true || true || true === true // continue the loop

What we really want to check is whether the input is invalid. The input is invalid when it does not match at least one of K, Q, J, or A. Or better stated, when not K AND not Q and not J and not A.

We simply change our conditions from || to &&. Same example

Usr_Card != "K" // false
Usr_Card != "Q" // true
Usr_Card != "J" // true
Usr_Card != "A" // true

false && true && true && true === false // do not continue the loop

while (Usr_Card != "K" && Usr_Card != "Q" && Usr_Card != "J" && Usr_Card != "A")

Let's try an invalid example. Use enters an "A"

Usr_Card != "K" // true
Usr_Card != "Q" // true
Usr_Card != "J" // true
Usr_Card != "A" // true

true && true && true && true === true // continue the loop

So as you can see, we only continue the loop when we don't match any of the valid characters thanks to our AND && operation.

And as Scary Wombat has pointed out, you should not use == or != with strings. You should use .equals

while( !Usr_Card.equals("K") && !Usr_Card.equals("Q") && !Usr_Card.equals("J") && !Usr_Card.equals("A") )

Upvotes: 3

Arjun Kay
Arjun Kay

Reputation: 328

Change the condition in the while loop to:

while (Usr_Card.equals("K") && Usr_Card.equals("Q") && Usr_Card.equal("J") && Usr_Card.equals("A"))    

EDIT: As pointed out by @ScaryWombat in the comments, method of string comparison changed from '!=' to equals().

Upvotes: 0

glenebob
glenebob

Reputation: 1973

This expression,

(Usr_Card != "K" || Usr_Card != "Q" || Usr_Card != "J" || Usr_Card != "A")

always evaluates to true, therefore the loop with never exit.

You need AND logic here, like:

(Usr_Card != "K" && Usr_Card != "Q" && Usr_Card != "J" && Usr_Card != "A")

Upvotes: 0

Related Questions