• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

compare

The code bellow retrieves value from textfield control.
However only the:


                       else
                       {
                               lb3.setText("Hello,"+tf1.getText());
                       }

is invoked.

How do I solve the problem?.



       public void actionPerformed(ActionEvent ae)
       {
               if(tf1.getText().trim().length()!=0 && tf2.getText().trim().length()!=0)
               {
                       String name = tf2.getText();
                       name = name.trim();
                      
                       if(name.toUpperCase()=="male")
                       {
                               lb3.setText("Hello,Mr."+tf1.getText());
                       }
                       else if (name.toUpperCase()=="female")
                       {
                               lb3.setText("Hello,Ms."+tf1.getText());
                       }
                       else
                       {
                               lb3.setText("Hello,"+tf1.getText());
                       }
               }
               else
               {
                       JOptionPane.showMessageDialog(this,"You must enter date in both fields","Message",JOptionPane.INFORMATION_MESSAGE);
               }
       }


Your help is kindly appreciated.

Regards

Eugene
0
eugene007
Asked:
eugene007
  • 3
2 Solutions
 
objectsCommented:

>    if(name.toUpperCase()=="male")

use equals() instead of ==

   if(name.toUpperCase().equals("male"))

same for other cases

0
 
objectsCommented:
== tests if they are the same object, equals() test whether objects have the same value
0
 
JakobACommented:
and remember to write "male" and "female" in the case you convert to.

name.toUpperCase().equals( "FEMALE" )

otherwise it will definity never be equal.
0
 
objectsCommented:
good pickup, I missed that :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now