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.
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];
}
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.
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.
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Quote:
Originally Posted by
Tolls
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..
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
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.. :=-:
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
no, you printed the array value. print the value of i.
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Oh i see
displays 0-9. So now it's correct?
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
It's not incorrect... what are you trying to do?
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()
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Quote:
System.out.println((int)Math.random()*11);
Does no one notice you can just do:
Code:
Random ran = new Random();
System.out.println(ran.nextInt(10)+1);
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Quote:
Originally Posted by
SJF
(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.
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.
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? :s: 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 :)-:
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.
Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
Quote:
Originally Posted by
paperclip
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.