Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions