Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String.compareTo(String)

 
Alex Kirk
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an odd little problem: two strings which appear to the naked eye to be identical are showing up as different when I use java.lang.String.compareTo(String).
The relevant code is:
...
byte good[] = welcome.getData();
String goodStr = new String(good);
String want = new String("GDNP OK\n\n");
System.out.println(want);
int wasGood = goodStr.compareTo(want);
System.out.println(wasGood);
if (wasGood == 0)
{
didOK = true;
System.out.println("Got welcome string");
}
else
{
System.out.println("Handshake failed: packet was " + goodStr);
}
My output reads thusly:
GDNP OK

1
Handshake failed: packet was GDNP OK

Couldn't connect; dying
This doesn't make any sense to me at all. Just in case it's relevant, welcome -- where my first String is coming from -- is a DatagramPacket.
Any ideas why this wouldn't work?
Alex Kirk
 
Rene Marot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I don't knwon if it comes from the function you use to compare string but you can try to display each byte of your good[] array so you can see what they realy are. You may have an unvisible charater like asc(000) witch is different of a space but sometime looks like on screen.
Bye.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic