String isn't equal to the same string?

Hi there JGO community,

I’ve a small bug if I even can call it a bug. I am currently working on a chat-system. Not that different as the Login, disconnect, move packet. But i want to make a server that say who just connected etc. So I thaught: Just change the name to SERVER and draw it different:

GameServer:

			Packet03Chat packet03 = new Packet03Chat("SERVER", ((Packet00Login)packet).getUsername() + " has connected.");

Chat (render):

for(int i = 0; i <= 11; i++){
				if(chatMessages.size() - i > 0){
					int colour = 555;
					System.out.println(chatNames.get(chatNames.size()-i - 1));
					if(chatNames.get(chatNames.size()-i - 1) == "SERVER"){
						if(typingMessage){
							Font.render(chatMessages.get(chatMessages.size()-i - 1), screen, 10 + screen.xOffset, 120 + screen.yOffset - i * 10, Colours.get(000, -1, -1, 324), 1);
						} else {
							Font.render(chatMessages.get(chatMessages.size()-i - 1), screen, 10 + screen.xOffset, 130 + screen.yOffset - i * 10, Colours.get(000, -1, -1, 324), 1);
						}
					}
					if(typingMessage){
						Font.render(chatNames.get(chatNames.size()-i - 1) + ":" + chatMessages.get(chatMessages.size()-i - 1), screen, 10 + screen.xOffset, 120 + screen.yOffset - i * 10, Colours.get(000, -1, -1, colour), 1);
					} else {
						Font.render(chatNames.get(chatNames.size()-i - 1) + ":" + chatMessages.get(chatMessages.size()-i - 1), screen, 10 + screen.xOffset, 130 + screen.yOffset - i * 10, Colours.get(000, -1, -1, colour), 1);
					}
				}
			}

I thaught this would work, and even didn’t needed to think of anything other. But it didn’t worked so I did some System.out.println, and saw some “SERVER”'s had a space: "SERVER " (Which is kinda weird because in the chat it is without a space…). So I removed the last space if their were any:

if(name.lastIndexOf(" ") == name.length()){
			name = name.substring(0, name.length() -1);
		}

Then I debugged it again and the System.out.println was only saiying: “SERVER”. So basically it worked, but it didn’t.
Does anyone know what I am doing wrong?

Screen:

http://img826.imageshack.us/img826/6505/ke3r.png

If you want more information just ask I will give it.

Thanks for helping me and being a good community,
Sincerely with much love, RoseSlayer

Well, == cannot be used when comparing strings - .equals needs to be used. That, to me, looks like the problem, though I may be seeing incorrectly.

Uhmm, wow that was really dumb of me… Thanks for the answer!

-RoseSlayer

Looks the same to me as well. Your problem is definitely with using ‘==’ instead of ‘.equals’.

Just a heads up.

Since String is an Object equal must be used.

To compare Objects , always use .equal(object)

Source : SCJA Book

:smiley:

You can’t use the equals operator ([icode]==[/icode]) on strings since Java strings are not primitives but they are objects. (Notice the capital S in [icode]String[/icode])

So you have to use the [icode]equals()[/icode] method of that string.

An example.


String str1 = "A string";
String str2 = "A string";

System.out.println(str1 == str2);       // Prints false
System.out.println(str1.equals(str2));  // Prints true

Both will return true, as the string is interned.

@Riven

You are right. Sorry for that. This answer from StackOverflow specifies this example.


// These two have the same value
new String("test").equals("test") ==> true 

// ... but they are not the same object
new String("test") == "test" ==> false 

// ... neither are these
new String("test") == new String("test") ==> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" ==> true 

// concatenation of string literals happens at compile time resulting in same objects
"test" == "te" + "st"  ==> true

// but .substring() is invoked at runtime, generating distinct objects
"test" == "!test".substring(1) ==> false

Thanks for pointing that out.

Thanks for all the help, that is probably why I messed Objects up so often. I’ve read online much about Strings, but it didn’t covered up the Objects part. So thanks for helping and learning me more about programming in Java.

How it looks now:

http://img29.imageshack.us/img29/9306/ku6f.png

-RoseSlayer