1

I have the following problem: I have a String that I read from a MySQL Database (with the JDBC driver) and have to compare it with another String that I receive over TCP with a BufferedReader. When I use String.equals, false is returned even if the Strings are exactly equal, I even printed both Strings to the console to make sure there aren't any typos or null objects. String.compareTo returns a negative number around -100.

I am really confused here and have no concrete idea how to fix that. Maybe it's related to the database's encoding (UTF-8)?

As requested, here is my code snippet:

public TeleportSign getTeleportSign(String target) {
    // I used a HashMap, but I switched to an ArrayList in order to be able
    // to compare the Strings directly.
    //return signs.get(target);
    for(TeleportSign s : signsList) {
        // I am printing the Strings here. I even put stars to the left
        // and the right of the String to make sure there are no
        // spaces or new lines. s.getTarget() returns the String from the DB,
        // target is the String sent over TCP.
        System.out.println("*" + s.getTarget() + "* " +
                String.valueOf(s.getTarget().compareTo(target))
                + " *" + target + "*");
        if(s.getTarget().compareTo(target) == 0)
            return s;
    }
    return null;
}

And the console output is:

*TDM1* -84 *TDM1*

Thanks in advance! Captain

Captain Kopp
  • 149
  • 1
  • 5
  • 3
    Whats the string values?? Please add the code snippets – Srikanth Balaji Mar 09 '17 at 11:49
  • 1
    What are the two string values? – Bathsheba Mar 09 '17 at 11:49
  • 2
    Print out the numerical character value of every char in both Strings. You'll notice that they merely *seem* equal to the untrained eye! – Kayaman Mar 09 '17 at 11:49
  • 3
    Try something like System.out.println(Arrays.toString(foo.toCharArray())); and post the result here... Could be padding with spaces, case difference, simiar character etc. 1 vs I ... – Adam Mar 09 '17 at 11:49
  • Is there a carriage return at the end of one of them? Are they the same length (`String.length()`). – Steve Smith Mar 09 '17 at 11:51
  • To add to the above: If you compare using for example "toUpper" - there may be issues with some chars, for example the turquish "i". – Fildor Mar 09 '17 at 11:57
  • Are you giving the `UTF-8` encoding to the `BufferedReader`? – Jorn Vernee Mar 09 '17 at 12:03
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Malakai Mar 09 '17 at 12:04
  • Print out the character values with `System.out.println(Arrays.toString(target.toCharArray()));` for both Strings. – Kayaman Mar 09 '17 at 12:17
  • I would suspect a non-printing character in one of the strings. @SteveSmith and Adam’s suggestions would be likely to reveal. – Ole V.V. Mar 09 '17 at 12:18
  • 1
    The output -84 would agree with a null character from the first string being compared with the capital T from the other. – Ole V.V. Mar 09 '17 at 12:20
  • 1
    Nope, @Reborn, hasn’t got anything to do with that question. The asker is using `equals()` as s/he should. – Ole V.V. Mar 09 '17 at 12:23
  • @OleV.V. That was probably the problem, even if I don't know why a system reboot fixed the error. I will check that in my code in future anyways, just to make sure everything works fine. Thank you! – Captain Kopp Mar 09 '17 at 12:25

1 Answers1

0

So I rebooted the entire system and retried. Everything works now as expected. I can't explain this to myself because I restarted the JVM multiple times and nothing happened, and a system reboot shouldn't affect a Java program like this.

I am sorry for everyone's time I wasted, but I really appreciate your quick help anyways.

EDIT: I use the trim method from String now. This method cuts off any leading null characters to prevent issues like this one. I hope this will be helpful for someone who has the same problem!

Captain Kopp
  • 149
  • 1
  • 5
  • Most likely something with you database or JDBC driver (since I suspect a null character from there). Thx for reporting back. – Ole V.V. Mar 09 '17 at 12:31