0

I'm adding in a loop, but the return value is always 0, I can't figure it out.

If I uncomment one of the final 2 lines, it returns the manual value correctly, and the rest of the class works. ARRAYLIST_SIZE = 10.

public float averageBearing() {
    float sumBng = 0;
    for (int i = 0; i==ARRAYLIST_SIZE; i++) {
        Location l = locList.get(i);
        float tempBearing = l.getBearing();
        sumBng += tempBearing;
    }
    float finalBng = sumBng/ARRAYLIST_SIZE;
    //remove following lines  for real use
    //finalBng = (float) (Math.random()*360);
    //finalBng = (float) 105.0;
    return finalBng;
}

I am reasonably sure the locations in the list have bearings, here is the add method. I have to spoof the bearing for now because the location only has it if we're moving, but I'm at my stationary desk.

public void add(Location location) {
    if (locList == null) {
        locList = new ArrayList<Location>();
    }
    //test code to spoof bearing
    location.setBearing((float) 105.0);
    //use only locations with extra data
    if (location.hasBearing() && location.hasSpeed()) {
        locList.add(location);
        mostRecent = location;
        //ensure we have at most 10 recent locations
        //no reason to use stale data
        while (locList.size()>10) {
            locList.remove(0);
        }
    }
    ARRAYLIST_SIZE = locList.size();

}
3
  • Why initialize a new tempBearing ever loop? Commented May 11, 2013 at 20:52
  • 1
    I suggest that you learn how to use the Eclipse or IntelliJ IDEA debugger. These are essential tools for any Android developer. They allow you to step through your code one line at a time to see what it is doing. You can also view the values of all of your variables to make sure they are what you expect. Commented May 11, 2013 at 21:02
  • I'm using ADT on Eclipse, I'll look into it. The software has so many tools, I need to spend a day and just learn the environment... Commented May 11, 2013 at 21:07

3 Answers 3

3

The conditional expression in your loop is testing equality. It fails on the first test, since i is zero, and ARRAYLIST_SIZE is 10. Change it to this:

  for (int i = 0; i<ARRAYLIST_SIZE; i++) {
Sign up to request clarification or add additional context in comments.

2 Comments

OK, thanks. Looks like I need to review for loops, I thought it was saying "start i at 0, and until it equals SIZE, add 1 to i and loop." I guess its saying "as long as i = SIZE, loop" which obviously never happens.
Right. Before each iteration, the conditional expression in the middle is tested. The loop ends the first time it's false.
3

Change this:

for (int i = 0; i==ARRAYLIST_SIZE; i++)

to this:

for (int i = 0; i<ARRAYLIST_SIZE; i++)

Comments

1

you never enter the for loop because of

i==ARRAYLIST_SIZE

it should be

i<ARRAYLIST_SIZE

1 Comment

The idea is right, but using <= instead of < will result in an IndexOutOfBoundsException

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.