-4

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

Community
  • 1
  • 1

2 Answers2

0

You check just the first element and exit the program if its not found. Instead try looping for the rest of the elements.

Also, you can't use remove() in a list of Object (of type ClientNode) and pass a String. This means you're trying to compare a ClientNode Object with a String, which is not possible. So List is not removing any elements.

Recommend to use Iterators in such scenarios.

Change:

for (ClientNode client : srv.connections) {
 if (Objects.equals(client.getIpAddr(), clientAddr)) {
            srv.connections.remove(clientAddr);
        } else {
            System.out.println("Provided address is not found!");
            System.exit(0);
        }
    }

To Something like:

boolean isFound = false;
for (Iterator<ClientNode> iterator = srv.connections.iterator(); iterator.hasNext();) {
    ClientNode client = iterator.next();
    if (Objects.equals(client.getIpAddr(), clientAddr)) {
        isFound = true;
        iterator.remove();
        break;
    }
}
if(!isFound) {
    System.out.println("Provided address is not found!");
    System.exit(0);
}

If you don't want to use a flag, then on first if condition write a return;
outside the loop blindly write a System.out.println("Provided address is not found!");

Kishore Bandi
  • 5,537
  • 2
  • 31
  • 52
  • Hi @Bandi thanks for answering. I tried your answer, not woring for me. Output of display after running deleteNode() is still showing 3 clients. – giovanni_m Apr 23 '17 at 15:34
  • @giovanni_m There was one more error, you were trying to perform a removal of types which don't match. I've modified the answer to make it work. – Kishore Bandi Apr 23 '17 at 15:45
  • Ah, it works! So all I needed was a way to iterate over the items within the ArrayList? Thanks @Bandi – giovanni_m Apr 23 '17 at 15:50
0

Question has been answered with help from user Bandi Kishore below. My deleteNode() method was not dealing with the right types, and was fixed with the help of an iterator.