I am taking a beginner Java course and working on arrays. My assignment is to generate a specified amount of random integers between two values inputted by the user.
This is my code:
// Variables
int amount,ur,lr,range;
System.out.println("Enter the amount of random numbers to generate::");
amount = Integer.parseInt(myInput.readLine());
System.out.println("\nEenter the upper range:");
ur = Integer.parseInt(myInput.readLine());
System.out.println("\nEnter the lower range:");
lr = Integer.parseInt(myInput.readLine());
// Create a new array that holds up to user inputted amount of variables
int[] generate = new int[amount];
// Create a range of numbers the array can randomly select as its value,
// given the user's input of the lowest and highest values available to be selected
range = (int)(Math.random() * ur) +lr;
System.out.println("\nGENERATED NUMBERS");
System.out.println("=================");
// Loop to print randomized numbers up to the amount the user inputted
for (int n=0; n < generate.length; n++){
// Give the array the value of the range
generate[amount] = range;
// Output [amount] variables
System.out.println(generate[amount]);
}
The error I get is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at randomnumbers.RandomNumbers.main(RandomNumbers.java:42)
Java Result: 1
If anyone could explain why this occurs/what it means, and how to fix, that would be helpful.
The exception is due to
generate[amount] = range;
System.out.println(generate[amount]);
This should be
generate[n] = range;
System.out.println(generate[n]);
This is because your array has a size amount
so it goes from position 0 to amount-1, thus you cannot use or assign to generate[amount].
Moreover, you should generate the range
number every time since this is only one random number. So the range = (int)(Math.random() * ur) +lr;
should be inside the loop
You're indexing the array, generate
by a variable, amount
, whose value doesn't change once it has been initially set from the user's input. Think again about how to reference array elements and you'll probably see why this won't work.
3 things that will need corection:
0
till lengtOfArray - 1
, so for array of 10 elements last element will be under array[9]
. You never want to use something like array[sizeOfArray] = ...
because it will be to big number. Math.random()
will never return 1 (it will return values from 0 till almost 1), so think your randomizing formula again. for
loop. I believe you want to move it into for loop to get more random numbers in your array. Rather than using Math.Random() I'd suggest you create an actual Random object since you gonna generate the number more than once as
Random randomNumber = new Random();
Then inside your loop you do what the other comments above suggest
for(int n=0;n<generate.length;n++)
{
range = lr +randomNumber.nextInt(ur+1);
generate[n] = range;
System.out.println(generate[n]);
}
I'm sure this will cover the base...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.