0

This code will not perform the if statement on the multiple ORS (i.e. || ) and I can't figure out why, can someone help? New to Java.

    if (!"A123".equals(itemNumber) || !"A55".equals(itemNumber) || !"B750".equals(itemNumber) 
                                   || !"C650".equals(itemNumber))
    {
        System.out.print("\nInvalid Item. Try again. ");
        System.out.print("\nEnter an item number or END to exit: ");
        itemNumber = keyboard.nextLine();
    }
    System.out.printf("\nThank you. %s", itemNumber);
2
  • 8
    try to change || to && Commented Jun 28, 2018 at 2:36
  • 1
    May even want to reduce the if statement by using !Arrays.asList("A123", "A55", ...).contains(itemNumber) Commented Jun 28, 2018 at 2:37

3 Answers 3

2

In your code you are asking "if item number is not equal to "A123" OR "A55" OR "B750" OR "C650", then do these things". So when you input "A55", program checks that is "A55" not equals to "A123" or "A55".... Ohh It's not equal "A123" so the condition is true.

If you change what you ask like this. "if item number is not equal to "A123" AND "A55" AND "B750" AND "C650", then do these things". So when you input "A55", program checks that is "A55" not equals to "A123" and "A55"... Ohh it's not "not equal" to "A55" so the condition is false. Example,

if (!"A123".equals(itemNumber) && !"A55".equals(itemNumber) && !"B750".equals(itemNumber) && !"C650".equals(itemNumber))
{
    System.out.print("\nInvalid Item. Try again. ");
    System.out.print("\nEnter an item number or END to exit: ");
    itemNumber = keyboard.nextLine();
}
System.out.printf("\nThank you. %s", itemNumber);

Or you can change what you asking to something like this, "if item number is equal to "A123" or "A55" or "B750" or "C650", then print "Thank You" and else do these things." It would be correct. Example,

if ("A123".equals(itemNumber) || "A55".equals(itemNumber) || "B750".equals(itemNumber) || "C650".equals(itemNumber))
{
    System.out.printf("\nThank you. %s", itemNumber);
}
else{
    System.out.print("\nInvalid Item. Try again. ");
    System.out.print("\nEnter an item number or END to exit: ");
    itemNumber = keyboard.nextLine();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You probably want to use the 'AND' operation instead of 'OR':

if (!"A123".equals(itemNumber) && !"A55".equals(itemNumber) && !"B750".equals(itemNumber) && !"C650".equals(itemNumber))
{
        System.out.print("\nInvalid Item. Try again. ");
        System.out.print("\nEnter an item number or END to exit: ");
        itemNumber = keyboard.nextLine();
}

System.out.printf("\nThank you. %s", itemNumber);

which translate into "if itemNumber is not equal to A123 AND itemNumber is not equal to A55 AND itemNumber is not equal to B750 AND itemNumber is not equal to C650"

An even better approach would be to use the switch statement:

switch(itemNumber)
{
  //fall through all acceptable case
  case "A123":
  case "A55":
  case "B750":
  case "C650":
    System.out.printf("\nThank you. %s", itemNumber);
  break;

  // no acceptable match found
  default:
    System.out.print("\nInvalid Item. Try again. ");
    System.out.print("\nEnter an item number or END to exit: ");
    itemNumber = keyboard.nextLine();
   break;
}

2 Comments

Yep, switch or Arrays.asList is a lot cleaner
Thanks guys, but this problem requires me not to use Arrays. The instructor wants to use the while loop, test user input validation on the "item Numbers" and output a total...the if statement keeps pushing to "thank you" statement regardless if I type A123 or A555 (or any other non item input)...when I use the &&, the II's are worse. I'll try the switch
1

Your mistake is you use 'or' Operation. you should use 'AND' Operation for it.. also if you use Arrays.asList with contains() method for this problem , I think it is easy way to this.

if (!"A123".equals(itemNumber) && !"A55".equals(itemNumber) && !"B750".equals(itemNumber)  && !"C650".equals(itemNumber))
{

    System.out.print("\nInvalid Item. Try again. ");
    System.out.print("\nEnter an item number or END to exit: ");
    itemNumber = keyboard.nextLine();
}

    System.out.printf("\nThank you. %s", itemNumber);

1 Comment

it is a nice explanation

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.