• 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

Why are two matching strings returning as NOT matching?

 
Ranch Hand
Posts: 73
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure exactly why my two text strings are returning as NOT matching when they DO match? If you can open it, the JPG attachment illustrates that both strings are matching but the code below returns that they DO NOT match.

this.CurrentSelectionData.get(0) is an ObservableList in which array position [0] was used to populate the tbxSearchQuestion textbox (a JavaFX-SceneBuilder control) at the time of its population. What the code is designed to do is compare what the user changed in the  tbxSearchQuestion back to what the original value in the ObservableList this.CurrentSelectionData.get(0) just to make sure there was a change. I included a 'trim()' just to make sure there is not any leading or trailing spaces, but it still returns not matching when they do.

Any thoughts?


StringComparison.JPG
[Thumbnail for StringComparison.JPG]
String Comparison
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use the "==" operator to compare strings for equality. Use the equals() method instead.

The == operator will tell you whether two references refer to the same object in memory. The String.equals() method tells you whether one string is the same as another.

When you use .trim() on a String, you're practically guaranteeing that a different String object is created so the == operator will most likely result in false. Bottom line is use equals() when comparing objects with one another for logical equivalence.
 
Scott Vallery
Ranch Hand
Posts: 73
2
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! That worked. Thanks Junilu!
 
Author
Posts: 18
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott, I see that Junilu already answered your question.  Thanks Junilu.

I wanted to add that there is also a method: which comes in handy if you want to compare the Strings but are not concerned if the capitalization matters.  For example:


Happy coding!

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic