
-----------------------------------
king
Sat Nov 19, 2005 7:15 pm

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!!!!!!!!

-----------------------------------
Hikaru79
Sun Nov 20, 2005 12:03 am


-----------------------------------
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.
myString = "Hello";
if (!myString.equals("Goodbye")){
     System.out.println("This if statement will return true!");
}
Simple, no? :) Here's the general idea:!true == false

-----------------------------------
Hikaru79
Sun Nov 20, 2005 12:04 am


-----------------------------------
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
Tue Nov 22, 2005 1:42 pm


-----------------------------------
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: 
 
//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
Wed Nov 23, 2005 12:34 pm


-----------------------------------

 if (name1 ^ name2) 
 

uhm... that's not proper Java..

What Hikaru said was right.. that's wrong.
