# Need help using Arrays!

Printable View

• February 4th, 2012, 04:47 PM
asb23698
Need help using Arrays!
I have been working on this code for awhile now and I almost have it except I cant figure out how to get rid of the "java.lang.ArrayIndexOutOfBoundsException" at lines 21 & 43(shown In parentheses). I have to generate 100 random numbers between 0-9 and count how many times each number comes up. Can I not take this approach or is there something that needs tweaked?? My full code is below, thank-you!

public static void main(String[] args) {
// TODO code application logic here
int [] numbers = createArray();
System.out.println("The Numbers are:");
displayArray(numbers);
(21) int[] counts = countNumbers(numbers);[/COLOR]
System.out.println();
System.out.println("The occurence of each number are: ");
displayCounts(counts);
}
public static int[] createArray(){
int[] numbers = new int[100];
for(int i=0; i<numbers.length; i++)
numbers[i]= (int)(Math.random()*10);
return numbers;
}
public static void displayArray(int[] numbers){
for (int i = 0; i<numbers.length; i++){
if((i+1)%20 == 0)
System.out.println(numbers[i]);
else
System.out.print(numbers[i] + " ");
}
}
public static int[] countNumbers(int[] numbers){
int[] counts = new int[10];
for (int i =0; i <numbers.length; i++)
(43) counts[numbers[i] + '0']++;
return counts;
}
public static void displayCounts(int[] counts){
for (int i =0; i<counts.length; i++){
if ((i+1)%10 == 0)
System.out.println(counts[i] + " " + (int)(i + '0'));
else
System.out.println(counts[i] + " " + (int)(i + '0') + " ");
}
}
}
• February 4th, 2012, 05:04 PM
pbrockway2
Re: Need help using Arrays!
If you are getting an ArrayIndexOutOfBoundsException with "counts[numbers[i] + '0']++;" then it might be a good idea to check with System.out.println() which array index you are trying to access.

Code :

```public static int[] countNumbers(int[] numbers){ int[] counts = new int[10]; for (int i =0; i <numbers.length; i++) { System.out.println("About to access counts array at index " + (numbers[i] + '0')); counts[numbers[i] + '0']++; } return counts; }```
• February 4th, 2012, 05:32 PM
asb23698
Re: Need help using Arrays!
The indexes producing the problem seem to change each time the program runs, but it always seems to be in the range of indexes 48-55. What does this tell me?
• February 4th, 2012, 05:47 PM
pbrockway2
Re: Need help using Arrays!
Quote:

Originally Posted by asb23698
The indexes producing the problem seem to change each time the program runs, but it always seems to be in the range of indexes 48-55. What does this tell me?

Well, the first thing it tells you is what you already knew: you have a problem! Because the array itself only has 10 slots in it so index values in the range 48-55 will break things.

So basically your formula numbers[i]+'0' for calculating the index is wrong and has to be changed. You are wanting to calculate an index value in the correct range: zero to nine.

Think about what counts is supposed to represent, and what index you should use for a given element in the numbers array. Perhaps more System.out.println() might help. (Remember while you're debugging the program you don't actually need 100 numbers.)

Code :

```public static int[] countNumbers(int[] numbers){ int[] counts = new int[10]; for (int i =0; i <numbers.length; i++) { System.out.println("Found " + numbers[i] + " in the numbers array") System.out.println("About to update counts array at index " + (numbers[i] + '0')); counts[numbers[i] + '0']++; } return counts; }```

What you are looking for is something more appropriate than numbers[i]+'0' to use as an array index.