Search code examples
javaif-statementnetwork-programmingserverkryonet

Comparing value of transmitted objects


I am trying to make a simple irc server and client, and I am trying to find a way to have the server respond differently to certain messages sent to it. I am using the kryonet api for networking. Here is my listener:

        public void received(Connection c, Object o){

            if (o instanceof TransferMessage){

                TransferMessage msg = (TransferMessage)o;

                if (msg.text == "Hello"){

                    System.out.println("User said Hello to me!");

                }
                System.out.println(tag+msg.user+": "+msg.text);
                server.sendToAllTCP(msg);

            }

        }

I have created a client program which sends the message "Hello" upon connecting. However, the server fails to recognize that msg.text does in fact equal "Hello", or some other anomaly is happening. There is no error produced, but the server ignores the conditional statement. I have also verified that the string that the client sends is what it is supposed to be.


Solution

  • == compares references. To compare values, try

    "Hello".equals(msg.text)