Results 1 to 7 of 7
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

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

    Java 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]);
    			}
    		}
    		
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Problem with arrayIndexOutOfBoundsExeption

    Compare your two for loops:

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

    See the difference?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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++?
    Last edited by wdh321; 11-17-2011 at 04:31 PM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Problem with arrayIndexOutOfBoundsExeption

    Quote Originally Posted by wdh321 View Post
    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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Problem with arrayIndexOutOfBoundsExeption

    Quote Originally Posted by KevinWorkman View Post
    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?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Problem with arrayIndexOutOfBoundsExeption

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

    What happened when you tried it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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 :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •