Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I overcome this random-number array out of bounds exception?

801490Nov 30 2008 — edited Nov 30 2008
Hi everyone,

I was wondering if anyone could help me with the following code.

At the moment the my code outputs the following:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -12
at coursework3.Exercise007Point007.main(Exercise3Point08.java:31)

If I run the program again I get the same message with a different random number after the ava.lang.ArrayIndexOutOfBoundsException.

Do I need to put some sort of Math abs into to code? Do I need to change an array size?

Thanks

WW
package
import java.util.Random;

/**
 * <dl>
 * <dt>Purpose:
 * <dd>Example program that randomly generates, collates and outputs numbers.
 *
 * <dt>Description:
 * <dd>This program generates 100 random numbers between 0 and 99 inclusive.
 * It also keeps count of how many times each number is generated and prints these 
 * counts out to the screen in a specified format.
 * </dl>  
 * @author  Woodie Woodpeck
 * @version $Date: 2008/11/30 14:44:00 $
 */

public class Exercise007Point007 
{
	public static void main(String[] args)
	{
			int[] counts=new int [100];
			
			for(int i=0;i<100;i++)
				counts=0;

Random r=new Random();

for(int n=0;n<100;n++)
{
counts[r.nextInt()%100]++;
}

double sum=0.0;
int maxCnt=0;
int mode = 0;
int runningCount=0;
int median = 0;

for (int i=0;i<100;i++)
{
sum+=(i*counts[i]);

if (counts[i]>maxCnt)
{
mode=i;
maxCnt=counts[i];
}

if (runningCount<50&&(runningCount+counts[i])>=50)
{
median=i;
}

runningCount+=counts[i];
}

System.out.println("Mean =" + (sum/100));
System.out.println("Median =" + median);
System.out.println("Mode =" + mode);
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2008
Added on Nov 30 2008
4 comments
624 views