View Single Post
  #2 (permalink)  
Old 08-07-2007, 07:02 AM
coco coco is offline
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
Ok, so as the error suggests you are trying to access an array index which is outside the bounds for that array. Take a look at the following code.
Code:
for (int range = 0; range < (salary.length); range++) { //Added this print statement so we can see why we're going out of bounds. System.out.println((int)Math.floor(((salary[range]*(.9)+200)*.01))); ++frequency[(int)Math.floor(((salary[range]*(.9)+200)*.01))]; }
You declare frequency as an array of size 11; which means the valid indexes for that variable are frequency[0] to frequency[10].
If you run your code with the added print statement and look at the numbers that get printed out you'll see that it prints 4,3,7,8,20 which are the numbers that are computed by (int)Math.floor(((salary[range]*(.9)+200)*.01)) for each iteration of the for loop. The error occurs at the computed value of 20.

If we simplify your code that means you're doing the following in your for loop
Code:
++frequency[4] ++frequency[3] ++frequency[7] ++frequency[8] ++frequency[20]
20 is not a valid index of the frequency array. As I previously stated since the array has a length of 11, trying to access the array at an index greater than 10 will give an array out of bounds error.

This suggests to me that your computation logic is slightly off.

Greetings.
Reply With Quote