Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Strings HELP WANTED
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
king




PostPosted: 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
Sponsor
sponsor
Hikaru79




PostPosted: 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? Smile Here's the general idea:
code:
!true == false
Hikaru79




PostPosted: 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 Sad
DanShadow




PostPosted: 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




PostPosted: Wed Nov 23, 2005 12:34 pm   Post subject: (No subject)

DanShadow wrote:

code:
if (name1 ^ name2)



uhm... that's not proper Java..

What Hikaru said was right.. that's wrong.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: