samedi 7 avril 2018

Why don't the two equal strings match?

I want to compare the response from the server with a string, but I get a false result when testing the two strings. Why?

I found this but didn't help: How do I compare strings in Java?

I tried two ways:

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if(Objects.equals((code = in.readLine()), "S")) { //Input string: "S"
    //code
}

 

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
String code;
if((code = in.readLine()).equals("S")) { //Input string: "S"
    //code
}

The code does not run in either case because the value of the test is false.

Aucun commentaire:

Enregistrer un commentaire