Hi,
if (!input.equals("Apple")|| (!input.equals("Orange")) {
System.exit(1);
}
The above has no compliation error. But it is not working as expected. Please advise how it needs to be written.
-Thanks
Hi,
if (!input.equals("Apple")|| (!input.equals("Orange")) {
System.exit(1);
}
The above has no compliation error. But it is not working as expected. Please advise how it needs to be written.
-Thanks
But it is not working as expected.
No computers do what you tell them to do, not what you want them to do.
What is it supposed to do? Can you explain the logic?
Think about this: If input is Apple then it is not Orange
Sorry! My bad.
If the input variable does not have Apple or Orange, I want the program to exit.
Read up on the logic truth tables.
What tests will make the whole conditional expression true?
Its AND vs OR
i'm still confused though is it : "does not have an apple or not an orange" or "not an apple or an orange"? and according to ur code it wont compile theres a bracket missing?!
according to ur code
I didn't post any code. I'm talking about logical expressions. You need to go back and read about how to compose a complex expression and how the computer evaluates it.
Hi,
The above has no compliation error. But it is not working as expected. Please advise how it needs to be written.
There must have a compilation error! Some thing like: ')' expected
the number of opned and closed bracket are not equals.
if (!input.equals("Apple")|| (!input.equals("Orange")) {
System.exit(1);
}
So your code may look like:
public class Test{
public static void main(String[] args){
String input=args[0];
if((!input.equals("Apple")) || (!input.equals("Orange")))
{
System.exit(1);
}
}
}
And there is also a logic error!
In all cases the
System.exit(1)
will be executed because this assersion
((!input.equals("Apple")) || (!input.equals("Orange")))
is alaways true.
May be you mean some thing like:
if(!((input.equals("Apple")) || (input.equals("Orange"))))
instead.
Hope it helps.
The OP needs to learn how to use the AND, OR and NOT logical operators.
That is what he/she is doing!
Thanks everyone for pondering. I overlooked and jumped.
I didn't see where anyone explained why the OP's code was wrong and why the new code is right.
I didn't post any code. I'm talking about logical expressions. You need to go back and read about how to compose a complex expression and how the computer evaluates it.
I never said you posted code??! i was referring to the original post -_-
I also had the same problem. And this is what I did.
if (input.equals("Apple") || input.equals("Orange")) {}
else System.exit(1);
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.