subject

Java Given an array of 10 names, complete the main method that outputs the name specified by the array index entered by the user. Use a try block to output the name and a catch block to catch any . Output the message from the exception object if an is caught. Output the first element in the array if the index is negative or the last element if the index is greater than the size of the array.
Hint: Format the exception outputs using the getMessage() method from the exception object. Do not hard code the exception messages.
Ex: If the input of the program is:
5
the program outputs:
Name: Jane
Ex: If the input of the program is:
12
the program outputs:
Exception! Index 12 out of bounds for length 10
The closest name is: Johnny
Ex: If the input of the program is:
-2
the program outputs:
Exception! Index -2 out of bounds for length 10
The closest name is: Ryley
import java. util. Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System. in);
String[] names = { "Ryley", "Edan", "Reagan", "Henry", "Caius", "Jane", "Guto", "Sonya", "Tyrese", "Johnny" };
int index;
index = scnr. nextInt();
try {
}
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 20:40
Peripherals are part of the main computer. true false
Answers: 3
question
Computers and Technology, 23.06.2019 15:20
In a game with three frames, where will the objects on layer 1 appear? a. next to the play area b. in the middle of the game c. behind everything else d. in front of everything else
Answers: 1
question
Computers and Technology, 23.06.2019 17:00
The more powerful, 60 volt cables and the main power shut off on an hev are both colored orange
Answers: 1
question
Computers and Technology, 23.06.2019 22:20
Learning sign language is an example of a(n) learning sign language is an example of a(n)
Answers: 2
You know the right answer?
Java Given an array of 10 names, complete the main method that outputs the name specified by the a...
Questions
question
Mathematics, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
question
English, 26.09.2021 01:10
question
Mathematics, 26.09.2021 01:10
Questions on the website: 13722361