-3

I want a user to input 4 things (first name, last name, email and phone number) and in case he misses one or more just one JOptionPane window would show.

 String firstname = TF_1.getText();
 String lastname = TF_2.getText();
 String email = TF_3.getText();
 String phonenumber = TF_4.getText();


 if (TF_1.equals("") ||
     TF_2.equals("") ||
     TF_3.equals("") ||
     TF_4.equals("")) {

 JOptionPane.showMessageDialog(null, "All text fields must be filled");
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
  • 1
    You should of course compare the text and not the field (e.g. `firstname.equals("")`) – Robin Aug 23 '16 at 09:47
  • 1
    Personally I'm annoyed by message dialogs. I like it more when the submit button is deativated until all textfields are filled. But thats a matter of taste. I wonder whats your actual question here? – ArcticLord Aug 23 '16 at 09:51
  • See the [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#stayup): "what if you want to check the user's answer before closing the dialog?" – Catalina Island Aug 23 '16 at 10:10

1 Answers1

0

First thing personally I don't like one generic message for all fields it puts users in confusion what they have not filled. You can use a flow that you check all field one by one and if a field is empty show error for it and return from their.

Secondly check on string fields firstname, lastname etc not on TF_1.

public boolean isEmpty(String... fields)
    {
        for (String string : fields)
        {
            if (null == string || string.trim().length() == 0)
            {
                // return true of filed is blank
                return true;
            }
        }
        return false;
    }

You can use this method and pass all your fields to check if any of them is empty.

Ankit Katiyar
  • 2,631
  • 2
  • 20
  • 30