0

i'm written a code that reads a line from the socket.
if the line is "bye" i want it to output "nice". from some reason it doesn't work, although i did try to output the input with no conditions and it did says "bye"

this is my code

 String input = null; if (socket.getInputStream().available() > 0) input = in.readLine(); if (input != null) { input = input.trim(); if (input == "bye") out.println("nice"); out.println(input); } 
1

3 Answers 3

8

Use String#equals(), not ==, to compare strings.

Sign up to request clarification or add additional context in comments.

Comments

0
if (input != null) { input = input.trim(); if (input.equals("bye")) out.println("nice"); out.println(input); } 

The equals() compares the content of string.

Comments

0

You want your inner if to say this:

if (input.equals("bye")) 

instead of

if (input == "bye") 

The == is actually comparing the reference object (sort of like a pointer) and may sometimes work when the string is interned, but that's another topic. Use .equals()!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.