Scamparelli
Scamparelli

Reputation: 744

What is wrong with this logic statement (Android)?

I can't see why the following logic isn't working:

                if (cursorCount > 1 && (!"x".equals(componentType) || !"y".equals(componentType))){
                        message.append("s");
                    }

So I want to to print 's' if the cursor count is over 1 but only when the componentType does not equal x or y..

Seems to work for the cases that are y but not x interestingly.

Confused.com! :)

Upvotes: 0

Views: 88

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83527

Try

if (cursorCount > 1 && !("x".equals(componentType) || "y".equals(componentType)))

Equivalently you can do

if (cursorCount > 1 && !"x".equals(componentType) && !"y".equals(componentType))

This comes from an application of deMorgan's law to your logic.

I believe these more closely matche your English description of what you want.

Edit:

To clear up the confusion, let's analyze the logic of the final part of your English description:

...but only when the componentType does not equal x or y.

Another way to state the same thing is "componentType is neither x nor y". In order to translate this into code, we should go a step further and reword this condition as "It is not the case that either componentType is x or comonentType is y." This final version suggests that the correct boolean formula is of the form

!(A || B)

This is very different from your original code which is of the form

!A || !B

Note that my final rewording is more verbose, but the extra verbiage makes the logic more clear.

Another way to analyze the logic is to look at the code you gave:

!"x".equals(componentType) || !"y".equals(componentType)

Let's look at a few examples:

  1. "x".equals(componentType) is true. This means the negation is false. It also means that "y".equals(componentType)` is false and it's negation is true. Therefore, your code evaluates to true.

  2. "y".equals(componentType) is true. This means the negation is false. It also means that "x".equals(componentType)` is false and it's negation is true. Therefore, your code evaluates to true.

  3. Neither "x".equals(componentType) nor "y".equals(componentType) is true. This means both negations are false and your code evaluates to false.

Note that your code evaluates to true in both cases 1. and 2. This does not give the same result as expected from your English description.

Upvotes: 5

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

if ((cursorCount > 1 && !"x".equals(componentType)) 
|| (cursorCount > 1 && !"y".equals(componentType))) 
  message.append("s");

or you could nest them if it'd make it easier

if (cursorCount > 1)
  if (componentType!="y" || componentType!="x")
    message.append("s"); 

This would make it easier to understand and with less fallacies.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

You have a '(' before your "x" condition that shouldn't be there. Then remove one of them from the end of the whole if statement. You should wrap those conditions that belong together in "()" because it is confusing as heck to read like this.

if ((cursorCount > 1 && !"x".equals(componentType)) || (!"y".equals(componentType)))

Upvotes: 0

Related Questions