File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String compare problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String compare problem" Watch "String compare problem" New topic
Author

String compare problem

Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
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?



Lawrence
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
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....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String compare problem