Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Check for empty String without if statement

I have a simple JFrame with three text boxes: First Name, Last Name and E-Mail address. Once the add button is pressed, the details are added to an Array List. Currently I have a group of if statements to check if the user has entered something in the text box as shown below:

private void addPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String firstName = firstNameTextField.getText();
    String lastName = lastNameTextField.getText();
    String emailAddress = emailTextField.getText();

    if (firstName.equals("")) {
        System.out.println("First Name is missing");
    } else if (lastName.equals("")) {
        System.out.println("Last Name is missing");
    } else if (emailAddress.equals("")) {
        System.out.println("E-Mail address is missing");
    } else if (!Email.isValid(emailAddress)) {
        System.out.println("E-Mail address is invalid");
    } else {
        personArrayList.add(new Person(firstName, lastName, emailAddress));
        System.out.println("Person added!");
    }
}

However, I find having long blocks of if statements makes the code difficult to read; it also won't alert the user about multiple text fields being empty. Is there a more efficient way of doing this?

Thanks in advance!

like image 822
Thomas Richards Avatar asked Dec 27 '22 08:12

Thomas Richards


2 Answers

More efficient? No.

More readable? Yep–efficiency of a different sort.

Create something like an isValid() method for each field, or field type. These will contain simple validations like blank checks, regexes, etc., and occasionally other domain-specific logic.

There are a ton of ways to break up and/or abstract this functionality, most roughly equivalent to:

private void addPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String firstName = firstNameTextField.getText();
    String lastName = lastNameTextField.getText();
    String emailAddress = emailTextField.getText();

    if (   isNameValid(firstName, "First name")
        && isNameValid(lastName, "Last name")
        && isEmailValid(emailAddress, "Email address")) {
        personArrayList.add(new Person(firstName, lastName, emailAddress));
        System.out.println("Person added!");
    }
}

The idea is to keep the mainline code clean and concise, in a way that fits with your personal style, the libraries you're using, etc. In addition to rolling your own, there are existing libraries to wrap this up.

If you have many "forms" like this it might make sense to abstract the form itself, and its validation; if it's just the one, there might not be a compelling reason to take it any further than something resembling the above.

like image 77
Dave Newton Avatar answered Dec 30 '22 11:12

Dave Newton


Just a little change that might help:

private void addPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String firstName = firstNameTextField.getText();
    String lastName = lastNameTextField.getText();
    String emailAddress = emailTextField.getText();
    boolean valid = true;

    if (firstName.equals("")) {
        System.out.println("First Name is missing");
        valid = false;
    }  
    if (lastName.equals("")) {
        System.out.println("Last Name is missing");
        valid = false;
    } 
    if (emailAddress.equals("")) {
        System.out.println("E-Mail address is missing");
        valid = false;
    } 
    if (!Email.isValid(emailAddress)) {
        System.out.println("E-Mail address is invalid");
        valid = false;
    } 
    if(valid) {
        personArrayList.add(new Person(firstName, lastName, emailAddress));
        System.out.println("Person added!");
    }
}
like image 36
MByD Avatar answered Dec 30 '22 09:12

MByD