The question is published on by Tutorial Guruji team.
I’m relatively new to programming in Java.
I’ve got a task for class in which I have to populate an array with random numbers from 0 to 100. However the user needs to be able to define the number of random numbers which are inserted within this array. For this I’ve created the variable ‘n’ for now.
I think I’ve got most of the code nailed. I wanted to experiment using the Random class to create the random numbers, as opposed to math.random*100.
I’m getting an error:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 10 at CreateArray.main(CreateArray.java:11)
Here’s my code:
import java.util.Scanner; import java.util.Random; public class CreateArray{ public static void main(String [] args){ int n = 10; Random myrandom = new Random(); int[] engine = new int[n]; // Initialising my array and giving it the name engine, I'm also passing value 'n' to my array length engine[n] = myrandom.nextInt(100-0); //This exclues 100 and 0, to include those I would say +100 or +0 for(int i=0; i < engine.length; i++){ //Using a for loop to cycle through my array and print out its elements System.out.println(engine[i]); //Printing out my array } } }
Answer
As for your error, the size of your array is n
, so the indexes in your array will be from 0
to n-1
. The code you have shown in your post tries to put a random variable in an index past the end of the array that does not exist (it is ‘out of the bounds’ of the array, thus the ArrayIndexOutOfBoundsException
). When n=10
, your array will have indexes 0
through 9
, and you are trying to put a value at index 10
.
Instead, you want to initialize each index in your array with a random integer, so you will want to loop through your array, like you are doing for printing the array, and create a new random variable for each index.
For generating random numbers, if you are using Java 1.7 or greater, you can use ThreadLocalRandom
. See this great answer to a similar question for the ‘proper’ way to generate a random number in Java (whether you’re using Java 1.7+ or not).
If using Java < 1.7, the ‘standard’ way to generate a random number in a range looks something like this:
public static int randInt(int min, int max) { //you can initialize your Random in a number of different ways. This is just one. Random rand = new Random(); int randomNum = rand.nextInt((max - min) + 1) + min; return randomNum; }
Note that in your code you mention that myrandom.nextInt(100-0);
will give you numbers between 0 and 100, excluding 0 and 100. That is not entirely correct. That will give you numbers between 0 and 99, including 0 and 99.
Since you know you only want numbers from 1 to 99, you can plug those in the method above to get this.
public static int randInt() { Random rand = new Random(); return rand.nextInt(99) + 1; }
Since this is basically a one-liner, it doesn’t really necessitate its own method, so you could generate your random numbers and store them in your engine array without the method, like this.
Random myrandom = new Random(); for (int i=0; i < engine.length; i++) { engine[i] = myrandom.nextInt(99) + 1; }
Finally, using Java 1.7+, you can use ThreadLocalRandom
, which you can use without initializing a Random
instance. Here is what using that would look like.
for (int i=0; i < engine.length; i++) { engine[i] = ThreadLocalRandom.current().nextInt(1, 100); }