Strings HELP WANTED
Author |
Message |
king
|
Posted: Sat Nov 19, 2005 7:15 pm Post subject: Strings HELP WANTED |
|
|
I am having trouble in loops
how can i specify if one string does not equal the other
i know how to specify if it equals eg
str.equals (str2));
HOW CAN I MAKE IT NOT EQUAL!!!!!!!! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Sun Nov 20, 2005 12:03 am Post subject: (No subject) |
|
|
The .equals(String s) method returns a boolean. So you can simply negate the entire expression. You do this by putting an exclamation mark in front.
Java: | myString = "Hello";
if (!myString. equals("Goodbye")){
System. out. println("This if statement will return true!");
} |
Simple, no? Here's the general idea: |
|
|
|
|
|
Hikaru79
|
Posted: Sun Nov 20, 2005 12:04 am Post subject: (No subject) |
|
|
Err, great. I accidently used a [/code] instead of [/syntax] and somebody has for some reason turned off editing in the help forums =/ I hope it's clear anyway.
Why *has* editing been turned off? It's mighty useful. I make mistakes all the time |
|
|
|
|
|
DanShadow
|
Posted: Tue Nov 22, 2005 1:42 pm Post subject: (No subject) |
|
|
Your typing was a little eratic, but as I understand, your looking to find out if one string is NOT equal to another?
Try this:
code: |
//Loop or whatever
String name1,name2;
name1="John";
name2="Alfred";
if (name1 ^ name2) {
System.out.println("name1 does not equal name2");
} else {
System.out.println(name1 does equal name2");
}
|
Hope that helps.-
[PS] Hikaru is also correct, the exclamation mark signifies a NOT statement, which would determine whether or not something is equal to something else. The '^' does nealry the same thing, except it will check that both sides of the argument (name1,name2) are not equal, and if they are not, it will return true, if they are, it will return false. |
|
|
|
|
|
rizzix
|
Posted: Wed Nov 23, 2005 12:34 pm Post subject: (No subject) |
|
|
DanShadow wrote:
uhm... that's not proper Java..
What Hikaru said was right.. that's wrong. |
|
|
|
|
|
|
|