Friday, 1 July 2016

java - If statement with String comparison fails

I really don't know why the if statement below is not executing:



if (s == "/quit")
{
System.out.println("quitted");
}


Below is the whole class.




It is probably a really stupid logic problem but I have been pulling my hair out over here not being able to figure this out.



Thanks for looking :)



class TextParser extends Thread {
public void run() {
while (true) {
for(int i = 0; i < connectionList.size(); i++) {
try {

System.out.println("reading " + i);
Connection c = connectionList.elementAt(i);
Thread.sleep(200);

System.out.println("reading " + i);

String s = "";

if (c.in.ready() == true) {
s = c.in.readLine();

//System.out.println(i + "> "+ s);

if (s == "/quit") {
System.out.println("quitted");
}

if(! s.equals("")) {
for(int j = 0; j < connectionList.size(); j++) {
Connection c2 = connectionList.elementAt(j);
c2.out.println(s);

}
}
}
} catch(Exception e){
System.out.println("reading error");
}
}
}
}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...