• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String compare problem

 
Lawrence Keeney
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program where I am inputing a character through an FTP connection. The character is located in the byte in[0]. I am converting it to a string using the String.valueOf.

I want to continue receiving characters until I receive a colon (

For testing, I wanted to print out the received character with the System.out.println statemet.

The problem is the compare is not working. It prints the value of the colon character as well as all other characters. What am I doing wrong?

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lawrence,

You can't (reliably) compare Strings using the == operator; you need to use the equals() function:



== checks to see if the two operands are the same physical location in memory; two Strings can have the same contents, but be physically distinct, and that's what the equals() methods checks for.
 
Lawrence Keeney
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thank you for the information, and your quick reply.

I am new to Java programming. I have taken a couple of on-line courses and bought and read a couple of books, I can't remember readding the fact that I could not compare strings with ==.

Back to the books....
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic