I'm still learning java programming so forgive my lack of knowledge. It probably is the most simplest thing ever, but I just can't for the life of me figure out a better way to have the checkTaskDescription method loop until the user input description is below the character limit. So far this is the only way I've been able to do it but it clearly repeats the input dialog twice.
This is the part that executes it
do{
checkTaskDescription();
}
while (checkTaskDescription() == false);
This is the checkTaskDescription method that is being executed:
public boolean checkTaskDescription() {
taskDesc = JOptionPane.showInputDialog(null, "Please enter a short description of the task");
if (taskDesc.length() > 50) {
JOptionPane.showMessageDialog(null, "Please enter a task description of less than 50 characters.", "ERROR",3);
taskDesc = JOptionPane.showInputDialog(null, "Please enter a short description of the task");
return false;
}
JOptionPane.showMessageDialog(null, "Task successfully captured.");
return true;
}
You need to use the results of your method call.
boolean ready = false;
do{
ready = checkTaskDescription();
} while ( ! ready );
That will enter the do block, assign ready to the return value of checkTaskDescription(), then check if it is false. If it is false, it will continue to loop, until it is true.
Your checkTaskDescription is also broken. If you fail the first time, it will show the dialog again, then always return false.
public boolean checkTaskDescription() {
taskDesc = JOptionPane.showInputDialog(null, "Please enter a short description of the task");
if (taskDesc.length() > 50) {
JOptionPane.showMessageDialog(null, "Please enter a task description of less than 50 characters.");
return false;
}
JOptionPane.showMessageDialog(null, "Task successfully captured.");
return true;
}
I updated the method so it would "work" when there is a failed input, it will show an error message, then loop and show the dialog again. This is why you should validate the data from the dialog, and not open the dialog in the loop.