Hello I\'m having trouble with the below code. Here is the parameters of the pro
ID: 3630458 • Letter: H
Question
Hello I'm having trouble with the below code.
Here is the parameters of the program:
Write a program that meets the following requirements:
*Create an array with 100 randomly chosen integers.
*Prompt the user to enter the index of the array, then display the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds.
This questions starts off with the following in bold (ArrayIndexOutBoundsException). I linked the textbook, it's chapter 13 question 13.1 if that helps.
-------------------------------------------------------
Here is my code so far. I'm getting an error under what I labeled line 28.
/*By Mario Gutierrez
* Array will generate with 100 randomly chosen integers
* If the specified index is out of bounds, display the message, Out of Bounds
*/
package array;
import java.util.Scanner;
public class ArrayException {
public static void main(String[] args) {
//Generate array with 100 randomly chosen integers
int [] rand = new int [100];
for(int i = 0; i < rand.length; i++)
rand[i] =
(int)(Math.random()*Integer.MAX_VALUE);
//Enter the index of the Array, then display the element
Scanner kb = new Scanner(System.in);
System.out.print("Enter an ");
try{
System.out.println(rand[kb.nextInt()]);
}
(28.) catch(ArrayIndexOutofBoundsException ex)
{
System.out.println("Out of Bounds");
}
}
}
-----------------------------------------------------
I appreciate you taking a look, and thanks in advance.
Explanation / Answer
please rate - thanks
you spelled of of instead of Of
/*By Mario Gutierrez
* Array will generate with 100 randomly chosen integers
* If the specified index is out of bounds, display the message, Out of Bounds
*/
package array;
import java.util.Scanner;
public class ArrayException {
public static void main(String[] args) {
//Generate array with 100 randomly chosen integers
int [] rand = new int [100];
for(int i = 0; i < rand.length; i++)
rand[i] =
(int)(Math.random()*Integer.MAX_VALUE);
//Enter the index of the Array, then display the element
Scanner kb = new Scanner(System.in);
System.out.print("Enter an ");
try{
System.out.println(rand[kb.nextInt()]);
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Out of Bounds");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.