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.)
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.