0

I'm new to Java, and I am creating a while-loop with one of the conditions being:

if ((userChoice != 'p') || (userChoice != 'P') || (userChoice != 's') || (userChoice != 'S')) System.out.println("*** Use P or S, please. ***");

Why is it that when I enter "p","P", "s", or "S" the program still outputs ""* Use P or S, please. *"??

This is the whole program:

import java.util.Scanner;

public class Foothill
{
   public static void main(String[] args)
   {
      // declare an object that can be used for console input
      Scanner inputStream = new Scanner(System.in);

      // declare variables
      String strUserInput;
      char userChoice, userCredits;
      int numYogurts, yogurtWallet = 0;
      // while loop for full transaction
      while (true)
      {
      // menu message
          System.out.println("Menu: \n P (process Purchase) \n S (Shut down)");
          strUserInput = inputStream.nextLine();
          userChoice = strUserInput.charAt(0);

        // condition that forces users to select only P or S
          if ((userChoice != 'p') || (userChoice != 'P') || (userChoice != 's') || (userChoice != 'S'))
              System.out.println("*** Use P or S, please. ***");

          System.out.println("Your choice:" + userChoice);
        // if condition that starts purchase part of transaction
          if ( (userChoice == 'p') || (userChoice == 'P') ) 
          {
              System.out.println("How many yogurts would you like to buy?");
              strUserInput = inputStream.nextLine();
              numYogurts = Integer.parseInt(strUserInput);

              yogurtWallet += numYogurts;

              System.out.println("You just earned " + numYogurts + " stamps and have a total of " + yogurtWallet + " to use");
              // if condition that tests number of purchased yogurts
              if (yogurtWallet >= 10)
              {
                  System.out.println("You qualify for a free yogurt. Would you like to use your credits? (Y or N)");
                  strUserInput = inputStream.nextLine();
                  userCredits = strUserInput.charAt(0);

                  if ((userCredits == 'Y') || (userCredits == 'y'))
                  {
                      yogurtWallet -= 10;
                      System.out.println("You have just used 10 credits and have " + yogurtWallet + " left. Enjoy your free yogurt.");
                  }
              }
          } 
          // if condition that stops the program 
          if ( (userChoice == 's') || (userChoice == 'S') )
          {
              System.out.println("Goodbye!");
              break;
          }

      }
   }
}
2
  • 7
    What does || do? Commented Oct 27, 2013 at 16:31
  • Change the || to && and it will work Commented Oct 27, 2013 at 16:33

4 Answers 4

4

Assume you entered p. Due to Short Circuit Evaluation, Java will continue and check all the conditions inside the if statement, the next check is != 'P', which is true! The same holds for the other inputs:

userChoice |  != 'p' | != 'P' | != 's' | != 'S'
-----------+---------+--------+--------+-------
    p      |   Yes   |   --   |   --   |  --
    P      |   No    |   Yes  |   --   |  --
    s      |   No    |   No   |   Yes  |  --
    S      |   No    |   No   |   No   |  Yes

-- means not evaluated because of Short Circuit Evaluation.

So in all cases, your if is satisfied!

Sign up to request clarification or add additional context in comments.

3 Comments

shot-circuit condition is the OPs issue and placeholder for bugs.
@nikpon Yes. That's what I clarified.
It will not check all conditions, it will output after the first due to true.
1

No matter which letter you choose, it will still be not equal to the other choices. || means OR. So in A OR B, if A is true, or B is true, then the entire condition is true. What you need to do is use &&, for AND.

1 Comment

A = B not equal to B = A the logic is not a Java but Java has logical operations.
0

use if ((userChoice != 'p') && (userChoice != 'P') && (userChoice != 's') && (userChoice != 'S'))

This will solve your issue.

1 Comment

can you please make it a practice to accept the answer that you use and works, it might help others too. :)
0
if ((userChoice != 'p') && (userChoice != 'P') && 
   userChoice != 's') && (userChoice != 'S')) 
       System.out.println("*** Use P or S, please. ***");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.