Hey lads. Doing a while loop question but am having a little difficulty with this question. I thought i knew enough to get questions like this correct but unfortunately not experienced enough. The following code is attempting to get sum of values in array that are less than 10. When i run program, it gives me an array out of bounds answer. Any ideas?

class q1 {
    public static void main(String[] args){
        int a[] = {1,3,14,6,12,2,14,5,8}; // declare array
        int i = a.length; 
        int sum = 1;

        while(a[i] < 10){ //while value in array is less than 10
            
            sum *= a[i]; // sum of 1 is multiplied with values less than 10
            i++;    // go up through values in array
        }
            System.out.println(sum);       // print out answer
    }
}

Walk thru your loop by hand looking at the values and the result of the while test. What happens when you reach the end of the array?

Walk thru your loop by hand looking at the values and the result of the while test. What happens when you reach the end of the array?

He's right that you need to work through your while loop in order to uncover the bugs. Btw your question and code don't match up. In your question you want to find the sum of all values under 10, but in the code you use the multiply symbol, why?

The reason you got the "Array out of bounds exception" error was because:

1) i is instantiated to the array length, which is 9. When you call it in line 7 at the beginning of the while loop it looks for a value that doesn't exist

For example, your array ranges from 0 through to 8. Your asking it to look at array position 9. It isn't there.

2) And for the actual code you would also require an if statement to check whether the value at that position is under 10. Something like the following will do the trick:

while(z < myArray.length){ 	
 
        	if(myArray[z] < 10)
        	{
            sum = sum + myArray[z];	 
        	}
            
        	z++;    			
           
        }
            System.out.println("Sum of array values under 10: " +sum);

I hope this helps mate. Remember to fine-comb your code!
Good luck :D

Oh and btw i didn't put in what z's initial value is. Its 0, as is the int value of sum. :P

Cheers for the info James and Katana.

I assumed by saying the sum of, that it meant the multiplication of the integers. I'm a bit of an idiot as you can tell ;)

Katana, thanks a million. I understand where I went wrong. The integer i in the array would count up to 9 which was the reason for it being out of bounds.

So you start the int z at 0 and run it through the length of array and then add the value in the array to the sum if it's less than 10.

You're an absolute legend. Any person i've sought advice from in the java section of the site has been very helpful. It's a pity i can't say that for other sections. Thanks again mate.

So you start the int z at 0 and run it through the length of array and then add the value in the array to the sum if it's less than 10.

With the correction that you run it through the length of the array - 1, since the length of the array is actually longer than the highest possible index of the array, as Katana mentioned before.

Since this thread has gone the way of suppling code rather than guidance, Java 5 (AKA version 1.5) contained improvements to avoid the horrible C-style code shown above. Far better to use:

for (int i : myArray) {
  if (i<10) sum += i;
}

(... especially when the number of values becomes variable and the array becomes an ArrayList)

This article has been dead for over six months. Start a new discussion instead.