Thread View: aus.computers.java
2 messages
2 total messages
Started by AKsteve
Thu, 04 Mar 2004 07:27
compare strings
Author: AKsteve
Date: Thu, 04 Mar 2004 07:27
Date: Thu, 04 Mar 2004 07:27
17 lines
1352 bytes
1352 bytes
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
Re: compare strings
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads