2

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"); 
3
  • 2
    You are setting the value, not checking it.what 's the problem with adding an if-statement to set the status? Commented Jun 27, 2015 at 7:43
  • if the first loop is fail,and the last loop is success.the result.setstatus() would be success. But actually, the first loop is fail. Commented Jun 27, 2015 at 7:46
  • as soon as a 'false' is encountered, you don't longer need to loop over the other elements, since you have your answer, so break out of the loop. Commented Jun 27, 2015 at 7:47

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't breaking out the loop when a false occurs be more efficiënt ? especially if work has thousands of elements and the first element is already false.
@Stultuske Only given a very strict interpretation of OP's stated requirements, specifically OP has stated what to do if the length is 4 and counter is 4 (or not). But nothing indicates those are the only requirements. Finally, an optimization of a break in the loop (and to check the work.length before begining the loop) might be in order; but it seems premature to me based on what OP asked.
In his question, he has: If 4 AResult all are true. so, as soon as one is false: the result is false.
@Stultuske Actually, the result is fail... but I take your point.
0

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

}

1 Comment

And if work contains three elements (or five elements)?
0

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

1 Comment

You can put a break if you want to come out of the loop if you get false the first time if(!AResult){ statusFlag = false; break; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.