Results 1 to 12 of 12
  1. #1
    paperclip is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    How do I make it in bound? I don't know how it is out of bounds in the first place.

    Java Code:
     public static void main(String[] args) {
           
           initialize(); 
    
       }
       
       public static int initialize(){
           int i;
           int[] arrItem1 = new int[10];      
           
          for (i = 0; i < arrItem1.length; i++){
             arrItem1[i] = (int)Math.random()*11;//random integer between 1 and 10
          }return arrItem1[i];
      }

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Please post the full error traceback next time. The error is on line 13 in your example.
    To see why, put a print statement inside your for loop, as well as before you return the value. That should hopefully clear it up.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    What is the value of 'i' after the loop has completed...

    ETA:
    Oh, and why return the last value stored in the array (presuming that's what you actually intend on doing)?
    What purpose does that serve?

    Indeed, that array does nothing at the moment, as it ceases to exist (as far as your code is concerned) as soon as that initialise method exits.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    paperclip is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Quote Originally Posted by Tolls View Post
    Oh, and why return the last value stored in the array (presuming that's what you actually intend on doing)?
    What purpose does that serve?
    Oh. er.. "Create an array of 10 integers in the main method, and pass the array to this method for initialization." and random number between 1 and 10, so..
    Java Code:
    arrItem1[i] = 1 + (int)Math.random()*11;
    instead?
    I will be passing this array of integers to other methods later on in the code for use.

    I printed it, I think.. using
    Java Code:
    System.out.println(arrItem1[i]);
    and it printed ten 0's, one 0 in each row...

    What is the full error traceback?
    Sorry, I'm not very good at this..
    Last edited by paperclip; 10-30-2012 at 09:46 PM.

  5. #5
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    no, you printed the array value. print the value of i.

  6. #6
    paperclip is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Oh i see
    Java Code:
    return i;
    displays 0-9. So now it's correct?

  7. #7
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    It's not incorrect... what are you trying to do?

    Java Code:
    		for(int i = 0; i < 100; i++){
    			System.out.println((int)Math.random()*11);//random integer between 1 and 10  --- Not really
    		}
    run this... you'll get 0s or 1s... not what you're expecting... HINT: (int)Math.random() = 0 or 1 (int)(expression) calculates the whole expression prior to typecast ;)

    Do you really want i as the return value? It will always be arrItem1.length after your for loop;
    ... and arrItem1 will only last for the duration of initialize()
    Last edited by SJF; 10-30-2012 at 10:40 PM. Reason: clarrification

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    System.out.println((int)Math.random()*11);
    Does no one notice you can just do:
    Java Code:
    Random ran = new Random();
    System.out.println(ran.nextInt(10)+1);

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Quote Originally Posted by SJF View Post
    (int)Math.random() = 0 or 1
    Actually it will always be 0. Math.random generates numbers from 0.0 inclusive to 1.0 exclusive. Which means 0.9999999999 will be the largest possible value and that will be 0 when cast to an int.

  10. #10
    paperclip is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Takes an array of integers and initialize each element with a random
    number between 1 and 10.
    Use: Create an array of 10 integers in the main method, and pass
    the array to this method for initialization.


    Java Code:
    public static void main(String[] args) {
                  
           initialize();      // Item 1
           printArray();      // Item 2
       }
       
       public static int initialize(){
           int i;
           int[] arrItem1 = new int[10];
           
           for (i = 0; i < arrItem1.length; i++){
               arrItem1[i] = 1 + (int)Math.random()*11;//random integer between 1 and 10
           }return i;
      }
       
       public static void printArray(){
           int i;
           int[] arrItem1 = new int[10]; 
           
           for (i = 0; i < arrItem1.length; i++){
               System.out.println(i);
           } //print arrItem1--------------------------
           
       }

    I seem to be misplacing something in the main method. Some sort of "shortcut" so I won't have to declare arrItem each time, maybe? I will be creating more arrays in this code later on.
    Also, it doesn't seem like my Math.random is working properly.. Right now it prints 0-9 in ascending order. I appreciate all the responses so far, thanks everyone

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    You have declared the array as a local variable in the initialize method. That means you cannot access it in the printArray method. You can either create an instance of the class and have the array and instance variable (attribute) which means all methods can access it. Or you can return the array from the initialize method and pass it as a parameter to the printArray method.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

    Quote Originally Posted by paperclip View Post
    Use: Create an array of 10 integers in the main method, and pass
    the array to this method for initialization
    .
    You are not creating the array in the main method, and you are not passing it to the initialise method.
    This is the root of your problem.

    The random number problem has been explained to you, so that needs changing as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. Replies: 4
    Last Post: 02-02-2011, 04:32 AM
  3. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

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