mercredi 28 août 2019

Why strings are not comparing [on hold]

I'm trying to compare two values using equals() that should be identical.

I've used normalizing methods such as toString() and trim() but nothing seems to allow these seemingly identical values to compare successfully. I know that there are a lot of ways to do compares, but why isn't this code working.

   public Boolean findUser(String username)  {
      Boolean found = false;
      String user= "";
      try {
          context.setRequestControls(null);
          NamingEnumeration<?> namingEnum = context.search("ou=users," + domainRoot, "objectClass=user", getSimpleSearchControls());
      while (namingEnum.hasMore() && found == false) {
         SearchResult result = (SearchResult) namingEnum.net();
         Attributes attrs = result.getAttributes();
         user = attrs.get("cn").toString().trim();
         if (user.equals(username.trim()))) found = true;
       }
     } catch (exception e) {
      e.printStackTrace();
     }
     return found
    }

If I print out both values they look identical. But if statement is never successful and therefore doesn't set found=true. I'm not looking for all permutations of how this code could be written differently I just want to know why it won't work as-is and how to fix given this approach. I realize that code could be shortened or tightened up.

Aucun commentaire:

Enregistrer un commentaire