Question
Write an application named BadSubscriptCaught in which you declare an array of 10 first names. Write a try block in which you prompt the user
Write an application named BadSubscriptCaught in which you declare an array of 10 first names. Write a try block in which you prompt the user for an integer and display the name in the requested position. Create a catch block that catches the potential ArrayIndexOutOfBoundsException thrown when the user enters a number that is out of range. The catch block should also display an error message. Save the file as BadSubscriptCaught.java.
Prompt the user to enter an integer and display the name in the array.
For example, if the first name in the array is "John" then when the user enters 0, simply print out index 0 of the array.
Use a try and catch block. If the user tries to access an index that is out of bounds (for example index 20) then display an error.
Use:
catch(ArrayIndexOutOfBoundsException error)
Also, if the user does NOT enter a number, catch that as well.
catch(NumberFormatException error)
Sample output:
Enter a number, and I will display a name g Not a number! Enter a number, and I will display a name 2 Name is Carla
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started