Hi, I have enrolled into Java classes and got this problem that i have mostly figured out how to do, but my application gives me an error when i run it. I understand that there is a mistake, but i can't figure out what is causing it and if it can be fixed.
The application is from a Java texbook, and its purpose is to calculate sales comissions.
at $200 base + 9% of sales = salary.
I'm using an array to feed the sales figures, then trying to do the math upon the values of the array and count how many salesmen fit into the ranges of salaries between:
200-299
300-399
....
1000 and more.
Here is my code:
public class Salary
{
public static void main(String[] args)
{
double salary[]={300, 200, 600, 700, 2000, 8000, 800, 800, 900, 6000, 1100};
int frequency []= new int [11];
System.out.printf("%10s: %6s:\n","Range", "Frequency");
for (int range = 0; range < (salary.length); range++)
++frequency[(int)Math.floor(((salary[range]*(.9)+200)*.01))];
for (int range1 =2; range1<frequency.length; range1++)
System.out.printf("$%4d-%4d: %10d\n", range1*100,range1*100+9,frequency[range1]);
}
}
Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Salary.main(Salary.java:11)
I know that if i just feed the numbers without doing the math, the application seems to work, but when i start doing the math, it stops working. I'm using the Math.
floor in order to get an integer value, which can then be pushed into the frequency array.
The frequency array should collect the values between 0 and 10 and then print them (as number of people) next to the range of salary.
Thanks