Jason
Jason

Reputation: 27

Java how to continue current loop when condition is false

I am having an issue whereby whenever input validation is incorrect it breaks out of my current loop and continues with the next method can anyone help me so that when condition is false it re-asks for the same input until it meets the condition. Here's my source code for my method class

public class Student {
    public int gradePt;
    public int i;
    public int credSum = 0;
    public double gradeCredSum = 0;
    public double gpa;
    String [] moduleName;
    String [] moduleGrade;
    int [] moduleCred ;
    Module[] modules;

    public void createModules(){

        getModuleNo();

         modules = new Module[i];
         moduleName = new String[i];
         moduleGrade = new String[i];
         moduleCred = new int[i];

         getModule();
         getGrade();
         getCred();

    for (int j = 0; j < modules.length; j++) {


       modules[j] = new Module(moduleName[j],moduleCred[j],moduleGrade[j]);
       }
    }

    public void getModuleNo(){
        do{
     String input  = JOptionPane.showInputDialog(null,
             "How many modules did you take?","Input");
           int a  = Integer.parseInt(input);
           if (a<1 || a>8){
               JOptionPane.showMessageDialog(null, 
                       "Invalid input please enter a number greater than 0",
                       "Error",JOptionPane.ERROR_MESSAGE);
               break;
    }      i = a;  
        }while(i<1 || i>8);  

}
    public void getModule(){
        for (int i=0;i<moduleName.length;i++){
             String input = JOptionPane.showInputDialog(null,
                    "Enter the name of module #"+(i+1));
             moduleName[i] = input;
             if (input == ""){
                 JOptionPane.showMessageDialog(null,
                         "Invalid input, module name cannot be blank","Error",JOptionPane.ERROR_MESSAGE);
                 break;
       }
    }

    }
    public void getGrade(){
        for (int i=0;i<moduleGrade.length;i++){
            String input = JOptionPane.showInputDialog(null,
                    "Enter grade (A,B,C,D,F) for module #"+(i+1));
             moduleGrade[i] = input;
             if (!"A".equals(input) && !"B".equals(input) && !"C".equals(input) && !"D".equals(input) &&
                     !"F".equals(input) && !"a".equals(input) && !"b".equals(input) && input!="c" &&
                     !"d".equals(input) && !"f".equals(input)){
                 JOptionPane.showMessageDialog(null,
                         "Invalid input!"+"\n"+"Please enter A,B,C,D or F","Error",JOptionPane.ERROR_MESSAGE);
                 break;
             }
             moduleGrade[i] = input;
      }
    }
    public void getCred(){

        for (int i=0;i<moduleCred.length;i++){
            String input = JOptionPane.showInputDialog(null,
                    "Enter credit units for module #"+(i+1));
             moduleCred[i] = Integer.parseInt(input);
             if (moduleCred[i]<1 || moduleCred[i]>8){
                 JOptionPane.showMessageDialog(null,
                         "Invalid input!"+"\n"+"Please enter a number form 1 to 8","Error",JOptionPane.ERROR_MESSAGE);
                 break;
             }

        }
    }

Upvotes: 0

Views: 186

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521103

I count four getter methods where you use a JOptionPane to gather user input. In the code snippet below, I have refactored getGrade() such that it continues to prompt the user for input, for a given module grade, until valid input is received.

While the fix for your three other methods is not given here, you can try following a similar pattern.

public void getGrade() {
    String invalidInputMsg = "Invalid input!" + "\n" + "Please enter A,B,C,D or F";
    for (int i=0; i < moduleGrade.length; i++) {
        do {
            String enterGradeMsg = "Enter grade (A,B,C,D,F) for module #" + (i+1)";
            String input = JOptionPane.showInputDialog(null, enterGradeMsg);
            if (input.length() == 1 &&
                "ABCDF".indexOf(input.toUpperCase().charAt(0)) != -1) {
                moduleGrade[i] = input;
                break;
            }
            else {
                JOptionPane.showMessageDialog(null, invalidInputMsg, "Error",
                                              JOptionPane.ERROR_MESSAGE);
            }
        } while(true);
    }
}

If the input be valid, then break will end the do loop, and the next value of i will be used in the for loop, should it be available. If the input be invalid, then the do loop will continue and prompt the user again for input.

Upvotes: 0

rossum
rossum

Reputation: 15685

This is a common process. You keep asking for input, checking it each time until the input is valid.

Pseudocode:

repeat
  display("Please enter your input: ")
  input <- getInput()
until (isValid(input))

Upvotes: 1

Rajiv Kapoor
Rajiv Kapoor

Reputation: 335

There are many loops in your program asking for input. so here is some code for your problem

boolean inputOk=false;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    do{
        // ask for input
        System.out.println("Enter the value");

        // capture input
        String input = reader.readLine();

        // evaluate input
        if(input.equals("ok"))
            inputOk = true;         // set flag if input is ok

    }while(!inputOk);               // if flag is ok then exit loop

Upvotes: 0

Related Questions