# Problem with arrayIndexOutOfBoundsExeption

• 11-17-2011, 05:06 PM
wdh321
Problem with arrayIndexOutOfBoundsExeption
Im creating a small program that generates a random number and stores it in in array. Im also checking whether the value is duplicated in the array before it is stored. The only problem is when i try and print the array afterwards to check the results i get the out of bounds exception.
This is if i put the for loop to loop through the array and print out on the outside of the main for loop.
Below you will notice the print loop is within the main for loop just to remove the outofbounds exception (I am fully aware of the masses of numbers this prints out if run :) )

So any help how i can just print the 50 values in the array after the numbers have been generated would be great.

Code:

```import java.util.Random; public class Test {         public static void main(String[] args)         {                 int[] number = new int[50];                 int nums;                 boolean found = false;                 Random randomGenerator = new Random();                 //Creates the 50 random numbers                 for (int i = 1; i <= 50; ++i)                 {                         nums = randomGenerator.nextInt(999);                                int targetValue = nums;                 //Checks if the random number is already in the array                         if(number[i] == targetValue)                         {                                 found = true;                         }                         else                         {                                 number[i] = targetValue;                         }                                                 for(int j = 0; j< number.length; j++)                         {                                 System.out.println(number[i]);                         }                 }                         } }```
• 11-17-2011, 05:17 PM
KevinWorkman
Re: Problem with arrayIndexOutOfBoundsExeption

for (int i = 1; i <= 50; ++i)
for(int j = 0; j< number.length; j++)

See the difference?
• 11-17-2011, 05:27 PM
wdh321
Re: Problem with arrayIndexOutOfBoundsExeption
Im using i and j. I did use i for both but for some reason it wouldnt work. . . I will try again.
Or are you referring to ++I and j++?
• 11-17-2011, 05:29 PM
KevinWorkman
Re: Problem with arrayIndexOutOfBoundsExeption
Quote:

Originally Posted by wdh321
Im using i and j. I did use i for both but for some reason it wouldnt work. . . I will try again.

That's not what I mean. Hint: How many indexes does each array have? What is the last index of each array?
• 11-17-2011, 05:33 PM
wdh321
Re: Problem with arrayIndexOutOfBoundsExeption
Quote:

Originally Posted by KevinWorkman
That's not what I mean. Hint: How many indexes does each array have? What is the last index of each array?

last index would be 49 not 50?
Should that then work?
• 11-17-2011, 05:38 PM
KevinWorkman
Re: Problem with arrayIndexOutOfBoundsExeption
Quote:

Originally Posted by wdh321
last index would be 49 not 50?
Should that then work?

'

What happened when you tried it?
• 11-17-2011, 06:55 PM
wdh321
Re: Problem with arrayIndexOutOfBoundsExeption
Hi Kevin,
Yes it works perfect now, i knew the array index started at 0 but for some reason didnt twig that was why it wasnt working, thanks for making me open my eyes :)