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