0

I have s simple UDP program where the server prints out the message from the client. However, if the client sends a phrase, in this case its "password", it should print another phrase ("123456").

However, when I send the string "password" using the client, the server does not recognise it and instead prints out "password".

May I know where I went wrong?

String sentence = new String( receivePacket.getData());
            if(sentence == "password"){
                System.out.println("123456");
            }
            System.out.println(sentence);
Jeugasce
  • 197
  • 1
  • 3
  • 11
  • Ah, hint: when you are so new to Java that you haven't learned that you use `equals()` to compare Strings, then seriously: writing code that does network communication is something you shouldn't worry about right now. Instead focus on learning these absolute essential basics .... – GhostCat Oct 24 '17 at 10:42

1 Answers1

0

Use the string.equals(Object other) method to compare strings, rather than the == operator

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29