I need help with my code below, I cant get the string comparison right with regards to the deleteNode method. I searched StackOverflow and How do I compare strings in Java? was helpful, especially with Objects.equals.
However, running the method falls through all if statements, and the item never gets deleted. PS, excuse my English, not native speaker.
My code:
public void deleteNode(String clientAddr) {
boolean isFound = false;
for (ClientNode client : srv.connections) {
if (Objects.equals(client.getIpAddr(), clientAddr)) {
isFound = true;
srv.connections.remove(clientAddr);
break;
}
}
if (!isFound) {
System.out.println("Provided address is not found!");
System.exit(0);
}
}
Above is called via:
starNetwork.deleteNode("10.10.10.3");
If I for instance run the following:
public void deleteNode(String clientAddr) {
for (ClientNode client : srv.connections) {
System.out.println(Objects.equals(client.getIpAddr(), clientAddr));
}
}
it outputs:
false
true
false
I don't understand