((Java) Return statement after if/else if/else loop?

16,804

Solution 1

You should add a return statement outside the loop in case numbers length is 0, so it will never enter inside your for function.

private static String HighOrLow (int[] numbers){
 for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] > average) {
         return (numbers[i] + "is above the average");
    }
    else if (numbers[i] < average) {
         return (numbers[i] + " is below the average");
    }
    else   {
         return (numbers[i] + " is equal to the average");
   }
  }
  return "No numbers founds";
}

Also by convention methods should start with the first letter lowercase:

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

Solution 2

If you wanted to evaluate all the numbers you should store the result for each integer in the array, let the loop finish and then return.

    private static String HighOrLow(int[] numbers) {
        if (numbers.length == 0) {
            return "No numbers provided";
        }

        ArrayList<Integer> above = new ArrayList();
        ArrayList<Integer> equal = new ArrayList();
        ArrayList<Integer> below = new ArrayList();

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] > average) {
                above.add(numbers[i]);
            } else if (numbers[i] < average) {
                below.add(numbers[i]);
            } else {
                equal.add(numbers[i]);
            }
        }

        StringBuilder result = new StringBuilder();
        result.append("Above Average: ");
        result.append(above.toString());
        result.append(", ");
        result.append("Below Average: ");
        result.append(below.toString());
        result.append(", ");
        result.append("Average: ");
        result.append(equal.toString());
        return result.toString();
    }
Share:
16,804
Michael Lewis
Author by

Michael Lewis

Updated on June 04, 2022

Comments

  • Michael Lewis
    Michael Lewis about 2 years

    Stuck on a particular part of my assignment: having a return statement outside of a loop. If you look at the code, you can kind of guesstimate what I'm trying to do here. The rest of the program works, but this method keeps giving me the same error statement: "missing return statement".

    private static String HighOrLow (int[] numbers)
     {
         for (int i = 0; i < numbers.length; i++)
         {
    
            if (numbers[i] > average) {
                 return (numbers[i] + "is above the average");
            }
            else if (numbers[i] < average) {
                 return (numbers[i] + " is below the average");
            }
            else   {
                 return (numbers[i] + " is equal to the average");
            }
    
        }
    

    I realize something's supposed to go outside the forloop, but not sure how to go about it. Any helpful help is appreciated.

    4/5/16: First of all, thanks for all the help! I got it set and ready to go..except every number, high or low, is pulling into above average. Paul Guiheen has the right idea and I appreciate it, but I have to print separate line for each array integer.

    • Eran
      Eran about 8 years
      It makes no sense to have a loop if your method returns after the first iteration. It's actually not clear what you are trying to do.
    • Matt C
      Matt C about 8 years
      Always be sure to include a description of what your code is trying to accomplish.
  • hansvb
    hansvb about 8 years
    If that is really the intent of the code, it would be clearer to change from for loop to if (numbers.length == 0) return "No numbers found" at the top of the method.
  • Michael Lewis
    Michael Lewis about 8 years
    Thanks for the additional help. Thing is I need it to properly read into the method. Format is printing up fine, but everything's coming up above average.