Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  String isn't equal to the same string?  (Read 585 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Devvie


Medals: 2



« Posted 2013-08-16 22:47:31 »

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:
1  
         Packet03Chat packet03 = new Packet03Chat("SERVER", ((Packet00Login)packet).getUsername() + " has connected.");


Chat (render):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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:

1  
2  
3  
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:


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

Fundamentum W.I.P.
Offline Sammidysam
« Reply #1 - Posted 2013-08-16 22:56:37 »

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.
Offline roseslayer

Junior Devvie


Medals: 2



« Reply #2 - Posted 2013-08-16 23:06:16 »

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

Fundamentum W.I.P.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-08-16 23:15:48 »

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.

Looks the same to me as well. Your problem is definitely with using '==' instead of '.equals'.
Offline Andre Lopes
« Reply #4 - Posted 2013-08-17 01:52:39 »

Just a heads up.

Since String is an Object equal must be used.

To compare Objects , always use .equal(object)

Source : SCJA Book

Cheesy
Online SHC
« Reply #5 - Posted 2013-08-17 02:46:16 »

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

So you have to use the
equals()
method of that string.

An example.

1  
2  
3  
4  
5  
String str1 = "A string";
String str2 = "A string";

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

Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2013-08-17 02:58:48 »

1  
2  
3  
4  
5  
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Online SHC
« Reply #7 - Posted 2013-08-17 03:51:24 »

@Riven

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
// 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.

Offline roseslayer

Junior Devvie


Medals: 2



« Reply #8 - Posted 2013-08-17 13:43:19 »

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:


-RoseSlayer

Fundamentum W.I.P.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!