Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 06:23 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
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.
Code:
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:
Code:
200-299 300-399 .... 1000 and more.
Here is my code:

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:

Code:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:02 AM
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main barney New To Java 1 08-07-2007 08:10 AM
Exception in thread "main" java.lang.NoClassDefFoundError carl New To Java 2 08-01-2007 06:26 AM
Exception in thread "main" java.lang.NoClassesDefFoundError tommy New To Java 1 07-31-2007 02:54 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM


All times are GMT +3. The time now is 10:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org