Sunday, 20 August 2017

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

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...