Home > Software design >  Can't figure out a proper way to do a loop that loops until input is valid
Can't figure out a proper way to do a loop that loops until input is valid

Time:06-07

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;
    }

CodePudding user response:

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.

CodePudding user response:

It will be much easier if you do something like this

public String getTaskDescription() {
    String taskDescription = "";
    do {
        taskDescription = JOptionPane.showInputDialog(null, "Please enter a short description of the task");
    }
    while (taskDescription.length() > 50);

    return taskDescription;
}

When you design your methods, try to make them as self contained as possible. Now you return a flag and rely on a caller method to call it again in case the input isn't valid.

CodePudding user response:

Here is another way of doing the same using while-loop

boolean flag = false; 
while(!flag){
  flag = checkTaskDescription();
}

The main idea is you always need to check what are you returning from the previous call.If checkTaskDescription() return false then call again else exit from the loop

  • Related