Your logic / understanding of the code is a bit off, let's try to fix it:
public double totalRent() {
int total = 0;
for(int index = 0; index < property.length; index++){
total += (property[index]).getRent(); //assuming the getter here
}
return total;
}
The first problem here was that you were returning a call to the method itself, thereby creating a recursion, and an infinite one at that, since you had no exit condition.
What you want to return are the values, not the function itself.
The function defines how you do something, the logical algorithm.
As you also stated, your IDE
tried to warn you that you were not using the value of total
. That is because within its scope (you can google this for tutorials and docs, or go here ) you never used it, so it was useless.
The solution is, you declare the variable outside of the for loop
, so that it is in scope and can be returned.
Inside of the for loop
, you progressively sum all the values, then return.
You will also notice I added .getRent()
right next to the call to each element of the array.
That is because you cannot sum objects directly with +
, you're gonna need to sum the correct field of each one. You can either get the field directly if it is declared as public
inside of the Property class
, or with a getter
(as I did),
ie. A method that specifically accesses and returns
a value from an object.
You can understand more about getters
and why they're useful by leraning the basic principles of encapsulation, or directly from the oracle tutorials here.
Of course, as some comments suggested, there are other ways to sum all the values in the array, however since this question was specifically about the mistakes you might have made, this is more than enough to learn.