0

I met a small problem when running a Java class that I wrote, though the design is pretty straightforward. I've created a JPanel, and I've added four JTextFields onto it and I've attached a button to this JPanel, too. Then, I've associated an ActionListener to this button being pressed. The code is like:

okButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) 
    {
       if (imageIdField.getText() == "" && 
           captionField.getText() == "" &&
           creditField.getText() == "" &&
           titleField.getText()== "") 
           {
              mediaXML = "";                        
              results.clear();
              results.put("error1", "more");

           } 
           else
           { ....
           }
    }

The strange thing is after I've pressed the OK button, and I did input text in those four JTextFields, still it will fall in the IF branch as if I didn't input any text in any of these four fields. I've been debugging this for a while, but no clue. Could anyone give me some hint like whether .getText() == "" is a valid way for testing no input?

Thanks in advance!

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
Kevin
  • 6,711
  • 16
  • 60
  • 107

6 Answers6

3

As has been mentioned, using == is not correct. For readability, try:

field.getText().isEmpty()

or

field.getText().trim().isEmpty()
1

Generally a bad idea to use == on Strings, or most other things. It checks that the objects are exactly the same instance, not that they have the same value. "" != new String("").

field.getText().equals("")

Or possibly better:

field.getText().isEmpty()
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
1

Use getText().equals("") instead of ==

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
1

Use == to check if it is the same object in memory, and .equals("YOUR STRING") to check if the content of the object is the same.

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
1

You should use .equals. Also, you might want to do something like this:

imageField.getText().trim().length() == 0  //The same for the others

or

imageField.getText().trim().isEmpty() //The same for the others

if you want to make sure that the user has actually written some characters instead of just white spaces.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • it removes all white space at both ends of the string – Kintaro Dec 13 '10 at 16:28
  • Yeah, in most cases you want to avoid having the user provide a bunch of white spaces as input. For instance for user names, you might want to have alpha numeric characters instead of a bunch of white spaces. – npinti Dec 13 '10 at 16:33
1

== only checks whether the left hand side and the right hand side refer to the exact same instance of an object. And since "" translates to something like new String(""), it will always return false, if you compare it with a string that already exists.

If you want to compare whether two instances of a class have the same state you need to use equals(). In your case *.getText().equals(""). A more elegant method would to use the isEmpty() method of the String class.

Kintaro
  • 1,287
  • 11
  • 17