user2492364
user2492364

Reputation: 6693

java: check boolean in for loop

If work.length is 4 , I have to check the AResult in for loop If 4 AResult all are true ,set result.setstatus("success"); or result.setstatus("fail");
What can I do ??

        for(int i = 0;i < work.length;i++){ 
            if(!work[i].contains("#")){                          
                CommandLineInterface CLI = new CommandLineInterface();
                String IP = null;
                boolean AResult;

                try {                       
                    AResult = CLI.Setting(work[i],"start"); //true or false                                     
                } catch (JSchException | InterruptedException e) {
                    e.printStackTrace();
                }       
            }        
        }
        //result.setstatus("success"); //all true
        //result.setstatus("fail"); 

Upvotes: 2

Views: 2077

Answers (3)

Madhuri M
Madhuri M

Reputation: 75

You can also try the following code

    boolean statusFlag = true; 
    for(int i = 0;i < work.length;i++){ 
        if(!work[i].contains("#")){                          
            CommandLineInterface CLI = new CommandLineInterface();
            String IP = null;
            boolean AResult;
            try {                       
                AResult = CLI.Setting(work[i],"start"); //true or false 
                if(!AResult){
                    statusFlag = false; 
                }
            } catch (JSchException | InterruptedException e) {
                e.printStackTrace();
            }       
        }        
    }
    if(statusFlag){
        result.setstatus("success"); 

    }else{
        result.setstatus("fail"); 
    }
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

Add a counter. Increment it when your condition is true. Check the value of the counter after your loop. Something like

int counter = 0;
for(int i = 0;i < work.length;i++){ 
    if(!work[i].contains("#")){                          
        CommandLineInterface CLI = new CommandLineInterface();
        String IP = null;
        boolean AResult;

        try {                       
            AResult = CLI.Setting(work[i],"start");
            if (AResult) {
                counter++;
            }                                     
        } catch (JSchException | InterruptedException e) {
            e.printStackTrace();
        }       
    }        
}
if (work.length == 4 && counter == 4) {
    result.setstatus("success");
} else {
    result.setstatus("fail");
}

You could optimize the above (and reduce the code size) with something like

int counter = 0;
if (work.length == 4) { // <-- check the length first
    for (int i = 0; i < work.length; i++) {
        if (!work[i].contains("#")) {
            CommandLineInterface CLI = new CommandLineInterface();
            try {
                if (CLI.Setting(work[i], "start")) {
                    counter++; // <-- increment the counter.
                } else {
                    break; // <-- break on any fale.
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
result.setstatus(counter == 4 ? "success" : "fail"); // <-- setstatus

Upvotes: 1

KDP
KDP

Reputation: 1481

try this, you really don't need to iterate the loop till end when a false condition is encountered in between

 //initially set success
 result.setstatus("success");
 for(int i = 0;i < work.length;i++){ 
if(!work[i].contains("#")){                          
    CommandLineInterface CLI = new CommandLineInterface();
    String IP = null;


    try {                       
       if(CLI.Setting(work[i],"start"))
         {
         result.setstatus("fail");
           //no need to iterate further
           break;
        }                                 
    } catch (JSchException | InterruptedException e) {
        e.printStackTrace();
    }       
}        

}

Upvotes: 0

Related Questions