It's not a secret anymore!
The moose likes Beginning Java and the fly likes Strings should be equal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings should be equal" Watch "Strings should be equal" New topic

Strings should be equal

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I'm going a bit mad trying to find out what is wrong with my code and I'd be grateful for any help.

Basically, I have a little program that should test my French vocabulary. I have got a list of vocab in an sql database. I retrieve this vocab and turn it into an ArrayList of VocabElement objects These objects basically have two instance variables, one string for the english and one for the french.

As each word comes to be tested I assign one to a private instance variable (String) in my VocabTester class called currentWord and the other to a private instance variable (String) called currentTargetWord.

I display one and then I should type in the answer and hit the submit button and as you can see from the code below, the currentTargetWord string should be compared with whatever has been typed into the answerTextField and if it's the right answer then I'm a linguistic genius!

Only problem is that even when I have the answer right (and believe me I have typed it exactly right) the program tells me I've got the wrong answer. I've put in a couple of println statements to see if the currentTargetWord and answer String are the same at that point of the program and they are!! So surely when the next statement says

the next block of code should run, shouldn't it? Does it make any difference that answer is a local variable and currentTargerWord is an instance variable or that currentTargetWord is private?

Please help!



Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Good question. To really understand why you are having this problem, check out this page. There is a way to compare string values, which is what you want to do. But what you are doing here is comparing string OBJECTS. They have the same value but are different objects, and that's why your program says they are not equal.

Studying for SCJP 6
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Eric

Thanks so much for the info. It's just what I needed. (As I'm sure you knew).


Joe :-)
I agree. Here's the link:
subject: Strings should be equal
jQuery in Action, 3rd edition