"java.lang.ArrayIndexOutOfBoundsException:" Problem...
Hello,
I'm receiving the folllowing error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
at numberCounter.main(numberCounter.java:32)
when attempting to run this code:
Code:
int i, j, n, max=50, number, t;
boolean located;
int [] nums =new int[50];
int [] count =new int[50];
System.out.print("How many numbers do you plan to input??");
n = Keyboard.nextInt();
for(i=0;i<n;i++)
{
System.out.print("Enter number " +(i+1)+": ");
number = Keyboard.nextInt();
located = false;
for(j=0;j<max;j++)
if(nums[j]==number)
{
located=true;
count[j]++;
System.out.println(j+" " + count[j]);
}
if(!located)
{
nums[max]=number;
count[max]=1;
max++;
}
}
System.out.println( +max+ " different numbers were entered");
System.out.println("In order from largest to smallest they are as follows: ");
for(i=0;i<max-1;i++)
for(j=i;j<max;j++)
if(nums[i]<nums[j])
{
t=nums[i];
nums[i] = nums[j];
nums[j] = t;
t = count[i];
count[i] = count[j];
count[j] = t;
}
System.out.println("N\t Count");
for(i=0;i<max;i++)
System.out.println(nums[i]+"\t"+count[i]);
}
}
End of Code:
Thanks,
Murd
Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...
For an array with a size of 50 the valid indicies are 0 - 49. You are trying to access element 50 which does not exist. Debug your code to find out why and correct it.
Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...
I think I understand what you're saying. Regardless it compiles correctly with the corrections I made. I appreciate your help. :(nod):
Thanks,
Murd
Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...
It might compile but AIOOBEs are generated at runtime.