Article View: aus.computers.java
Article #103Re: compare strings
From: Joshua King
Date: Thu, 04 Mar 2004 10:12
Date: Thu, 04 Mar 2004 10:12
37 lines
2292 bytes
2292 bytes
In <104dmijn8e95nfc@corp.supernews.com> AKsteve wrote:
> I am trying to compare two strings.
>
> If the username and password matches what I have stored then great...
> looks like this: if (user.equalsIgnoreCase("Steve") && password.equals(
> "iscool"))
>
> how do I check to see if it "does not equal" what I have stored.
>
> In other words, how do I check to see if a string does not equal
> another string. (Yeah, I know an if else statement could do it, but is
> there a simpler way - like .notequals)
>
> I want the program to check the username and password until it is
> entered correctly. I am using do/while. This is for a class assignment
>
> Steve
To check does not equal, you simply have to get the opposite by
inverting
the result from equals.
You need to use the NOT operator, which is ! (the exclamation mark).
It has the effect of flipping the result of a boolean expression, so
your example becomes:
if (!password.equals("iscool")) {
System.out.println("Invalid password!");
..
Note the exclamtion mark. If password is NOT "iscool" the equals method
returns false, which becomes true after the ! operator, and triggers the
if
statement.
Joshua King
Technical Details
Message-ID:
<20040304181209319+0800@news-server.bigpond.net.au>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news.moat.net!lon-transit.news.telstra.net!ken-spool.news.telstra.net!ken-in.news.telstra.net!ken-transit.news.telstra.net!news.telstra.net!news-server.bigpond.net.au!53ab2750!not-for-mailReferences:
<104dmijn8e95nfc@corp.supernews.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,1b8e7ee93b12f7f6 X-Google-Attributes: gid10d6db,public X-Google-ArrivalTime: 2004-03-04 02:14:07 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news.moat.net!lon-transit.news.telstra.net!ken-spool.news.telstra.net!ken-in.news.telstra.net!ken-transit.news.telstra.net!news.telstra.net!news-server.bigpond.net.au!53ab2750!not-for-mail User-Agent: Halime (MacOSX)/1.0rc2b MIME-Version: 1.0 References: <104dmijn8e95nfc@corp.supernews.com> Subject: Re: compare strings Content-Type: text/plain; charset="ISO-8859-15" Message-ID: <20040304181209319+0800@news-server.bigpond.net.au> Content-Transfer-Encoding: 8bit Organization: <none> From: Joshua King <jkingkard@hotmail.com> Newsgroups: aus.computers.java Lines: 37 Date: Thu, 04 Mar 2004 10:12:10 GMT NNTP-Posting-Host: 144.136.237.67 X-Complaints-To: abuse@bigpond.net.au X-Trace: news-server.bigpond.net.au 1078395130 144.136.237.67 (Thu, 04 Mar 2004 21:12:10 EST) NNTP-Posting-Date: Thu, 04 Mar 2004 21:12:10 EST