• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String compare problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic