Results 1 to 12 of 12
- 10-30-2012, 07:10 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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]; }
- 10-30-2012, 07:27 AM #2
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
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.
- 10-30-2012, 09:37 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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.
- 10-30-2012, 09:41 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
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..
instead?Java Code:arrItem1[i] = 1 + (int)Math.random()*11;
I will be passing this array of integers to other methods later on in the code for use.
I printed it, I think.. using
and it printed ten 0's, one 0 in each row...Java Code:System.out.println(arrItem1[i]);
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.
- 10-30-2012, 10:07 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 49
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
no, you printed the array value. print the value of i.
- 10-30-2012, 10:26 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Oh i see
displays 0-9. So now it's correct?Java Code:return i;
- 10-30-2012, 10:38 PM #7
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
It's not incorrect... what are you trying to do?
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 ;)Java Code:for(int i = 0; i < 100; i++){ System.out.println((int)Math.random()*11);//random integer between 1 and 10 --- Not really }
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
- 10-31-2012, 12:06 AM #8
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Does no one notice you can just do:System.out.println((int)Math.random()*11);
Java Code:Random ran = new Random(); System.out.println(ran.nextInt(10)+1);
- 10-31-2012, 01:13 AM #9
- 10-31-2012, 02:49 AM #10
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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
- 10-31-2012, 03:06 AM #11
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.
- 10-31-2012, 09:51 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Please do not ask for code as refusal often offends.
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By paul in forum New To JavaReplies: 8Last Post: 03-05-2011, 03:53 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at wee
By Azaz in forum New To JavaReplies: 4Last Post: 02-02-2011, 04:32 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
By dark_metal in forum New To JavaReplies: 5Last Post: 04-05-2010, 02:28 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks