Closed Thread
Results 1 to 3 of 3

Thread: Question about String handling...

  1. #1
    Spitfire is offline Newbie
    Join Date
    Jun 2007
    Posts
    4
    Rep Power
    0

    Question about String handling...

    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
    Code:
    if(txtField.getText() == STRING_VAR){
        somethingHappens();
    }
    When I try to use strings in this way, things never happen as they should.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    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.

  4. #3
    Spitfire is offline Newbie
    Join Date
    Jun 2007
    Posts
    4
    Rep Power
    0
    Thanks alot, as it would happen i was told about that shortly after i made the post lol

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. qbasic question (file handling)
    By Zer033 in forum Visual Basic Programming
    Replies: 1
    Last Post: 09-28-2010, 07:13 AM
  2. I have a question about string variables.
    By nmoghan in forum C# Programming
    Replies: 8
    Last Post: 09-11-2010, 01:16 PM
  3. String Array Question
    By PGP_Protector in forum C and C++
    Replies: 9
    Last Post: 12-03-2009, 04:06 PM
  4. handling text file and search a string using VB
    By pbosek in forum Visual Basic Programming
    Replies: 1
    Last Post: 02-25-2008, 07:49 PM
  5. String alignement question
    By SinXJon in forum Java Help
    Replies: 3
    Last Post: 02-15-2008, 01:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts