Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if text field is empty 1

Status
Not open for further replies.

stephenbruceburch

Technical User
Jun 1, 2002
11
0
0
US
I am having a problem and I believe this is where it lies. The program allows the user to identify an item by either its id or its location. I thought the easiest way to accomplish this was to just have both text fields in the panel and when the submit button was pressed check to see which was blank. that's where I'm having the problem (i think!)
I have tried these diffrent ways:
if (textField.getText() == null)
if (textField.getText() == " ")
if (textField.getText() == "")

Any help would be appreciated
 
Typically, when you are validating user input in Java, you will want to check for both null and empty string values. To check String objects for equality, you must use the .equals() method (not the == operator). Thus, a check for an empty String value might look something like this:

if ( val == null || val.trim().equals( "" ) )
{
// handle empty String case
}
else
{
// handle non-empty String case
}

Hope this helps,
Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top