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!
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.
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!");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With