I am trying to add a User into a LinkedList using Java, but I am wondering if there is a shorter way to check if multiple strings are empty instead of using if statements everytime I input a new string
This is the method I came up with
public void addUser() {
int error = 0;
do {
Users user = new Users();
String name = JOptionPane.showInputDialog(null, "Enter your name");
if (name.isEmpty()) {
error = 1;
} else {
String password = JOptionPane.showInputDialog(null, "Enter a password");
if (password.isEmpty()) {
error = 1;
} else {
// more string inputs
}
}
} while (error != 0);
}
Implement a separate method to read the input data until a valid string is provided and call this method with custom prompt:
private static String readInput(String prompt) {
String input;
do {
input = JOptionPane.showInputDialog(null, prompt);
} while (input == null || input.isEmpty());
}
public void addUser() {
String name = readInput("Enter your name");
String password = readInput("Enter a password");
// ...
User user = new User(name, password, ...);
// ...
}