Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-07-2007, 04:52 PM
Spitfire Spitfire is offline
Newbie
 
Join Date: Jun 2007
Posts: 4
Credits: 0
Rep Power: 0
Spitfire is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-07-2007, 08:41 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 3,211
Last Blog:
Passwords
Credits: 842
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

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 08:46 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-08-2007, 05:17 PM
Spitfire Spitfire is offline
Newbie
 
Join Date: Jun 2007
Posts: 4
Credits: 0
Rep Power: 0
Spitfire is on a distinguished road
Default

Thanks alot, as it would happen i was told about that shortly after i made the post lol
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB6.0:Tutorial, Error handling TcM VB Tutorials 9 06-06-2008 01:31 PM
need help in counting a specify character in a string. lichy Perl 2 06-27-2007 10:03 PM
Class question in Delphi tosh5457 Pascal/Delphi 4 06-11-2007 01:46 PM
[Help] String manipulation. afiser Visual Basic Programming 1 05-31-2007 03:24 PM
Is string in array? smith Perl 2 02-14-2007 01:30 PM


All times are GMT -5. The time now is 04:42 AM.

Contest Stats

Xav ........ 1097.16
MeTh0Dz|Reb0rn ........ 986.37
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 684.54
marwex89 ........ 638.26
Brandon W ........ 493.36
chili5 ........ 292.12
Steve.L ........ 188.37
orjan ........ 187.41

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads