I have been playing with Java for a few months now and i have a question. I have noticed that java seems to be a little tempremental with strings in logical statements. Im no expert but i im no newbie either, so i was wondering if someone could tell me how to do this. For example, using javas swing package, comparing the contents of a text field to a string variable to make something happen
When I try to use strings in this way, things never happen as they should.Code:if(txtField.getText() == STRING_VAR){ somethingHappens(); }
In Java strings are not primitive data types, they are objects, and can therefor not be compared using the binary operator [wingedpanther may know why]. When comparing strings in Java use the .equals() function.
Code:if(txtField.getText().equals(STRING_VAR)){ somethingHappens(); }
Last edited by John; 06-07-2007 at 06:46 PM.
Thanks alot, as it would happen i was told about that shortly after i made the post lol
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks