2

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";
  }
  }
1
  • 2
    There is no string[1345] defined in this code. Without the full code, and how myMethod is called, no help is possible Commented Jun 18, 2013 at 16:09

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Where have you defined string3, string4 etc ?
@TheNewIdiot I don't see his full class code, hence put that as side note.

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.