The moose likes Beginning Java and the fly likes Quicky String Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quicky String Question" Watch "Quicky String Question" New topic

Quicky String Question

Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Hi there.
Quick one I hope.
I'm trying to do a while loop, testing that a string is NOT equal to the user's input. I know how to do equals, but how would I do it for a not equal too.
As usually, I'm probably missing something right under my nose.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sue Hunt
Ranch Hand

Joined: Jan 26, 2004
Posts: 50
Thanks Paul
so I could do this then...
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

the "!" operator (i think its an operator, but in any case) simply flips ANY boolean value to the opposite value. ANYWHERE you have something that evaluates to TRUE or FALSE, you can stick the ! in front of it.
i assume mainQuery.equals("q") returns a boolean (if not, we have major naming issues ). therefore, yes, you can put your ! in front of it.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
s choukse

Joined: Dec 12, 2001
Posts: 27
You may like to consider this.
mainQuery.equals("q") will result in a NullPointerException if value of object "mainQuery" is null. It is always a good practice to compare in the following way
"sting literal".equals(sting_instance);
in you case
- Saurabh
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Or, along the same lines as the last post, become familiar with the so-called "short-circuit" operators in Java, || and &&. These are called short-circuit operators because you can short-circuit the remainder of a logical operation using them. Example:

You might expect the above to print out: "foo! bar!". But it doesn't. It'll never evaluate the foo() method because x is false, so it doesn't need to continue evaluating the rest of the logical AND statement.
This is useful mainly for null checks, though I suppose you could think of a thousand more bizarre uses:

I agree. Here's the link:
subject: Quicky String Question
It's not a secret anymore!