File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

String compare problem

Lawrence Keeney

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?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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

Joined: May 31, 2004
Posts: 17

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:
subject: String compare problem
It's not a secret anymore!