0

i try to make a menu in java and get user input Until user enter the right input, so i used while() in my code. but when i run my code , the only things is run is while loop for whole time that i enter input , even the right input.

//Show Menu Of Languages
    System.out.print("Welcome To iHome Application \n \n Pleas Choose You're Wanted Language" +
            "(Enter Number Of Language Or Type You're Wanted Language) : \n 1)English \n or \n 2)Persian " + "\n \n");
    //Get Input From User
    Scanner input = new Scanner(System.in);
    String MenuLanguage = input.next();
    //Get User Language Until User Enter The Right Format
    while (!MenuLanguage.equalsIgnoreCase("1") || (!MenuLanguage.equalsIgnoreCase("English")) ||
            (!MenuLanguage.equalsIgnoreCase("2")) || (!MenuLanguage.equalsIgnoreCase("Persian")) )  {
        System.out.print("\n Pleas Enter An Option From Above Menu. Try Again. \n");
        MenuLanguage = input.next();
    }
        if ((MenuLanguage.equalsIgnoreCase("1")) || (MenuLanguage.equalsIgnoreCase("English"))) {

        } else if ((MenuLanguage.equalsIgnoreCase("2")) || (MenuLanguage.equalsIgnoreCase("Persian"))) {
            System.out.print("Sorry. Persian Language Will Be Available Soon.");
        }

but always my output is : "Pleas Enter An Option From Above Menu. Try Again."

whats is my problem?

5
  • 4
    Replace || (OR) with && (AND) . Commented Mar 1, 2018 at 14:08
  • 3
    Change your ||'s To &&'s Commented Mar 1, 2018 at 14:09
  • 1
    Thanks .it fixed Commented Mar 1, 2018 at 14:09
  • 1
    but why i should use "And" instead of "Or"? when i'm tried to say if one condition was true. Commented Mar 1, 2018 at 14:11
  • 1
    You tried the opposite: If one condition is false, which is always true. Commented Mar 1, 2018 at 14:14

1 Answer 1

2

In your while loop, you check for OR (||). Replace it by AND (&&)

while (!MenuLanguage.equalsIgnoreCase("1") && (!MenuLanguage.equalsIgnoreCase("English")) &&
            (!MenuLanguage.equalsIgnoreCase("2")) && (!MenuLanguage.equalsIgnoreCase("Persian")) ) 

A condition cannot meet OR with different input. If it's one thing, it will not be the 3 other things. With and, you can check it's neither of the 4 conditions.

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

5 Comments

Thanks.but why i should use "And" instead of "Or"? when i'm tried to say if one condition was true?
A condition cannot meet OR with different input. If it's one thing, it will not be the 3 other things. With and, you can check it's neither of the 4 conditions. With the OR, only one condition needs to be True for the condition to consider the True path.
i cant understand , I'm trying to say of one of four condition was true , do something, not if all of my conditions was true. When i use " || " i say just one of my condition and when i use "&&" i say that is all of my conditions was true , and its cant be happen , because it isn't possible that one String is equal to four string together in one time.
I'll make an analogy : Imagine you have an apple you checked : if it's not a pear or not a kiwi or not a banana do something. But no matter what fruit you pass to that condition, it will never be 2 different fruits at once. So at least 2 out of those 3 conditions will say True. And if any of the conditions in an OR check says True, it passes.
i got it. Thanks

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.