0

In the following portion of code I make sure that, when the server returns an error message, the client shows it on the screen and, consequently, does not perform the operations within the if. The problem takes place when the server returns "Value not found". The client doesn't seem to recognize the returned string and remains in an infinite wait. I've already tried the .contains and .matches functions, but they don't work. Only comparing the length of the received string leads to the desired result.

ReceivedName = sIN.readLine();
System.out.println(ReceivedName);
if (ReceivedName.length() > 3) {          //ReceivedName = "N/D"; doesn't work.
    ReceivedSurname = sIN.readLine();
    System.out.println(ReceivedSurname);
} else
  continue;

The server performs these operations instead:

if (!found) {
   NameToSend = "N/D";
   sOUT.println(NameToSend);
   sOUT.flush();
   continue;
}
  • 1
    Did you try using `ReceivedName.toUpperCase().contains("VALUE NOT FOUND")`? This is to make the check agnostic of the case of `ReceivedName` . – Arvind Kumar Avinash Dec 07 '20 at 13:39
  • 1
    @ArvindKumarAvinash or [`equalsIgnoreCase`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#equalsIgnoreCase(java.lang.String)). But surely, an ordinary `equals` would work here, in contrast to `=`… – Holger Dec 09 '20 at 13:15
  • @Holger - Thank you. I agree and `equalsIgnoreCase` was in my mind when I wrote the comment but I do not know if there is something like `containsIgnoreCase`. I'm not sure if such a method was deliberately left out in the API or it's there somewhere which I am not aware of. – Arvind Kumar Avinash Dec 09 '20 at 14:11
  • 1
    @ArvindKumarAvinash before Java 5, there wasn’t even `contains`. I’d go with `Pattern.compile("foo", Pattern.LITERAL|Pattern.CASE_INSENSITIVE) .matcher(input).find()` if that’s really needed. – Holger Dec 09 '20 at 14:28
  • @Holger - I think your one-liner solution should solve OP's problem. Your every comment on my answer and comment is so valuable for me that I treasure each of them. I've bookmarked this question so that I do not lose it. – Arvind Kumar Avinash Dec 09 '20 at 14:54

0 Answers0