简体   繁体   中英

Returning a single string from a method with multiple loops in java

I am a newbie in Java and having difficulty in returning a single string from a method that has multiple for loops, each loop iterating 25 times and returning a pass/fail for each iteration. I'm trying to figure out what to do so if any of the for loops return a "fail" the over all method returns a fail, otherwise it just returns a "pass"? Following is a generic code I have.

    public String myMethod (String string, String string2) {

    for (int i = 0; i < 25 ; i++){
      B = string1 + i + string 2;
      if (B.equals("something"){
        return "Pass";
      }else{
        return "Fail";
    }

    for (int j = 0; j < 25 ; j++) {
     C = string3 + i + string 4;
      if (C.equals("something")){
        return "Pass";
     }else{
        return "Fail";
   }

   for (int k = 0 ; k < 25 ; k++) {
    D = string4 + i + string 5;
    if (D.equals("something")){
      return "Pass";
   }else{
     return "Fail";
  }
  }

if any of the for loops return a "fail" the over all method returns a fail

The for -loops do not return a value. Those return statements within the loops are returning a value for the entire method. This shouldn't really be a problem, however, as you can do something along the lines of

public String myMethod(String string1, String string2) {

    for (int i = 0; i < 25; i++) {
        B = string1 + i + string2;
        if (!B.equals("something"))
            return "Fail";  // we know the entire method should
                            // return "Fail" here
    }

    // other loops, same format

    return "Pass";  // we know nothing returned "Fail" at this point,
                    // so we return "Pass"

}

Note that this short-circuit-esque approach will be more efficient than maintaining a variable with the result of the method, as time will not be wasted continuing the method once we know what its return value should be.

Just use one variable with default as PASS . Set the value to FAIL when it fails and return in the end as below:

public String myMethod (String string, String string2) {
   String result = "Pass";
   for (int i = 0; i < 25 ; i++){
      B = string1 + i + string2;
      if (!B.equals("something"){
        result = "Fail";
      }
   }

   for (int j = 0; j < 25 ; j++) {
     C = string3 + i + string4;
     if (!C.equals("something")){
        result = "Fail";
     }
   }

  for (int k = 0 ; k < 25 ; k++) {
    D = string4 + i + string5;
    if (!D.equals("something")){
        result = "Fail";
    }
  }
  //return the final result, which is fail if it fails one ore more times
  return result;
 }

Side Note: Not sure of the declaration of String B, C, D String1..... etc. Take care of them if not already done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM