so the assignment is as follows:
I wrote an application in which you declare an array of eight first names. I wrote a try block in which you prompt the user for an integer and display the name in the requested position. I created a catch block that catches the potential ArrayIndexOutOfBoundsException thrown when the user enters a number that is out of range. The catch block also should display the error message Subscript out of range.
Here is what I have so far, but I am getting an error stating that I am missing “The BadSubscriptCaught program properly handles valid and invalid subscripts”. Any assistance would be great!
import java.util.Scanner;
public class BadSubscriptCaught
{
public static void main(String[] args)
{
String[] names = {“Ariel”, “Brad”, “Clifford”, “Denise”,”Emily”, “Fred”, “Gina”, “Henry”};
String integer;
try
{
System.out.println(“Please, enter an integer from 1 to 8 to display a name”);
Scanner input= new Scanner(System.in);
integer = input.nextLine();
int i = Integer.parseInt(integer);
i = i-1;
System.out.println(names[i]);
input.close();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(“Your number is out of range”);
}
}
}