1

I have a method to total up average results which calls another method which stores an array of the results but I seem to having trouble returning the array:

Array code:

static double findAverages() {
    double [] averagesArray = new double[10];
    for(int i = 0; i < 9; i++) {
        double total = (studentMarksArray[i][0]+studentMarksArray[i][1]+studentMarksArray[i][2])/3;
        averagesArray[i] = total;
    }
    return averagesArray;
}

Method calling array:

static void highestStudentAvgMark() {
    findAverages();
    double max = averagesArray[0];
    for (int i = 1; i < averagesArray.length; i++) {
        if (averagesArray[i] > max) {
            max = averagesArray[i];
        }
    }
    findMark(max, averagesArray);
    System.out.println(max);
}
1
  • 1
    in your case findAverages returns double but you DONT USE IT. and its array of double but not double value Commented Nov 29, 2013 at 15:52

2 Answers 2

3

You have defined double and not double [] as return type of your method.

Also averagesArray is a local variable in your findAverages() method, so it is not visible in other methods! You need to use the return value of findAverages():

static void highestStudentAvgMark() {
  double[] averagesArray = findAverages();
  double max = averagesArray[0];
  for (int i = 1; i < averagesArray.length; i++) {
    if (averagesArray[i] > max) {
      max = averagesArray[i];
    }
  }
  findMark(max, averagesArray);
  System.out.println(max);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That solves the error in findAverages but averagesArray is still not recognised in the highestStudentAvgMark method
@Colin747 You need to provide an access variable to the method call. double[] something = findAverages();
Thanks again, been a great help.
1

Your method signature says it returns double, but you are returning double[].

Also `findAverages();' is not stored locally, and so is not used in your second method.

Comments

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.