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 ArrayIndexOutOfBoundsException. Output the message from the exception object if an ArrayIndexOutOfBoundsException 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 {
}
}
}

Answers

Answer 1

The program illustrates the use of catching exceptions using try and catch.

Exceptions are simply errors that can crash a programTry and catch are used to prevent errors from crashing a program

The required try and catch block is as follows:

try {

       System.out.print(names[index]);

   }

   catch (ArrayIndexOutOfBoundsException e) {

  System.out.println(e.getMessage());

}

The flow of the above code segment is as follows:

First, we begin with the try block

try {

Then the program prints the element at the index

       System.out.print(names[index]);     }

If the index does not exist in the array (e.g. -1 or 11), then the catch block is executed

   catch (ArrayIndexOutOfBoundsException e) {

This prints the appropriate exception

  System.out.println(e.getMessage()); }

See attachment for sample run

Read more about similar programs at:

https://brainly.com/question/21330187

JavaGiven An Array Of 10 Names, Complete The Main Method That Outputs The Name Specified By The Array

Related Questions

If you were to design a real robot that would be responsible for cleaning up trash, what 5 (or more) commands would you give it and what would each one do?

Answers

Los 5 comandos de mi robot limpiador serían: Classify, recycle, sweep, washing, and alarm.

A robot is a machine that is characterized by being controlled by a computer and programmed to move, manipulate objects and carry out work while interacting with its environment.

The 5 commands with which I would configure my robot to clean the garbage would be:

Classify: To classify the garbage, deposit the recyclable wastes and those that are not in different bags.Recycle: Take recyclable objects and process them inside to turn them into new objects such as recycled sheets of paper.Sweep: For the robot to sweep a surface and remove all kinds of dirt.Washing: This function would be to clean the dirt from a car, motorcycle, or bicycle with a jet of water under pressure.Alarm: This functions as a reminder of the days and hours in which the garbage truck passes to take the garbage to the right place.

Learn more in: https://brainly.com/question/1935813

!!!!!HELP!!!!! (30PTS)
Suppose you are using a Mac to read your e-mail messages, and your receive an e-mail message with a PowerPoint file attached. PowerPoint is not installed on the Mac you are working with. Which of these could you use to view the file? Choose all answers that are correct.
A. a text editor
B. Keynote
C. PowerPoint Online
D. a photo editor

Answers

Answer:

B. KN

C. PP Online

Explanation:

KN is A's own software for creating slideshow presentations. However, since most people using KN are coming from using MS PP, there is the option to open PPs in KN (albeit with some fonts missing).

You can also view the file from PP Online. Just create a MS account (if you don't already have one), download the email attachment and upload it to your cloud, and you should be able to view it there.

Exercise 3.6.9 24 vs “24”

Answers

Answer:

that's good exercise

Explanation:

where is the question ⁉️

cyber law of nepal and it's provisions​

Answers

hope it helps .........

try drawing the bass clef sign by tracing over the dotted lines. then draw five more on your own

Answers

Answer:

ok

Explanation:

did it. It was a little difficult to keep my pencil straight.

Viết thuật toán và chương trình: Tính tổng:
S=1+2+...i

Answers

Answer:

skdjsjwjdjcdjejxjdjxjdjwjsjxjdwiskxmdjxkdnejxj

Explanation:

justnejedjdfhjxnfbcjccjcuvjjfhedcjcjchcjeidchdjchcjdjcjcucjf

Which decimal number is equivalent to this binary number?
001100112

A.
15
B.
51
C.
204
D.
240

Answers

Answer:

51

Explanation:

multiply the digits with the position value starting from Rightmost

0011 0011

(0*2^7) +(0*2^6)+(1*2^5) +(1*2^4)+(0*2^3) +(0*2^2)+(1*2^1) +(1*2^0)

solve the exponents

= 0+ 0 +32+16+0+0+2+1

=51

Help please answer the question 1 2 3 4 5 6

help

Answers

Answer: A,c,a

Explanation: trust

Just did it

Hello, this is for my compute science class, and I don’t know the answer, please help? Thank you in advance =)

Answers

Haven't taken Computer Science in a year so my answer may be wrong (APCSA).

Defining Column B,

Selection is when a piece of code is run only when its conditions are met. Iteration is typically represented by for loops, enhanced for loops, and while loops, and will all repeat until a condition is met.Sequence is usually the simplest, carrying out commands in order.

From what I see, the first box in column A is a sequence as it defines and casts num1 and num2, computes its difference in int form, and prints it out. The second box in column A is a selection even though it counts to 5 in the first segment of code. The key here is the if else statement as it checks that num1 is greater than num2 before it subtracts. The third box in column A is an iteration due to its enhanced for loop.

Hope that helps! :)

Who is responsible for the actions of autonomous systems? (please help me :C)

Answers

The responsibility for failures was deflected away from the automated parts of the system (and the humans, such as engineers, whose control is mediated through this automation) and placed on the immediate human operators, who possessed only limited knowledge and control.

Ohio university college

What should the car be programmed to do if it encounters an unavoidable accident? (about autonomous vehicles)(help me :c)

Answers

Answer:

The nation proposed that: “self-driving cars should always attempt to minimize human death and shouldn't discriminate between individuals based on age, gender, or any factor. Human lives should also always be given priority over animals or property” (Nowak).

state five differences and similarities between a desktop and a laptop computer​

Answers

Answer:

Difference:

1. screen size

2. Device needs external power source

3. Not portable

4. Much more powerful cores

5. Multiple internal drives

Similarities:

1. Can access the internet

2. Same operating system

3. components are similar

4. Applications can be run on both

5. They have the same purpose.

Which entry by the user will cause the program to halt with an error statement?
# Get a guess from the user and update the number of guesses.
guess = input("Guess an integer from 1 to 10.)
guess = int(guess)
seven
3
65
-25

Answers

Answer:

seven

Explanation:

Look at python data types

float=decimalint=integerstr=string

seven is a string not integer

So it will raise error

HELLLLLLLLPPP

Read the list of courses from a technical school (w3schools).

Which courses would prepare for you for a career in web development?
(Choose more than one answer)

Designing for Usability
Introduction to Networks
Technical Writing
Cybersecurity

Answers

I believe it’s all of them. They all help with the development of websites

Which is non executable statement used to write some information.​

Answers

Answer:

Programming command that is not run or executed when being read by the computer. For example, a commonly used nonexecutable statement is REM (remark) used in batch files and other Microsoft Windows and DOS programs.

You can change the size of text using which block of code?

text ("medium", 130, 200);

textSize (pixels);

text, ("large", 120, 300);

Answers

Answer:

B.

Explanation:

Mark Brainliest if correct!

the recovery of stored information is called _____.

Answers

Answer:

it is called the memory retrieval

The recovery of stored information is called memory retrievel.

What is information?

A broad concept that represents something with the potential to inform is known as "information." Information is most fundamentally concerned with the interpretation of what may be sense. Informational texts have facts and text components including glossaries, tables of contents, photos, captions, and bold print.

Retrievel is defined as a method of retrieving information from memory. Retrieval cues are stimuli that can help with memory retrieval. In a process known as "ecphory," memory retrieval involves the interaction of external sensory or internally produced cues with stored memory traces (or engrams). Ecphory has been studied in human cognitive neuroscience research, but less is known about its neurological basis.

Thus, the recovery of stored information is called memory retrievel.

To learn more about information, refer to the link below:

https://brainly.com/question/13629038

#SPJ12

What Is What is the difference between system software and application software?

Answers

Answer:

Hope it helps have a nice day..

what is the first step in the process of deploying a cloud computing offering?

Answers

Answer:

The first step in the process of deploying a Cloud Computing is to choose a suitable Cloud Provider such as AWS, Azure, or GCP. Once you have successfully chosen the cloud provider, you need to create an account to login and make use of the services of that provider.

Explanation:

yes but i’m and carter can go to

hey guys can u tell me

5 advantages of spread sheet
kinda urngent ?​

Answers

Explanation:

Spreadsheets Improve Data Organization.

Easing Collaborative Data Collection.

Spreadsheets Enable Dynamic Calculations With Built-In Formulas.

Spreadsheets Are Equipped With Preset Functions.

Spreadsheets Provide Functionality For Graphing And Visual Aids.

write a reflection on what you have learned for this week. write your answer on the space inside the cloud.(Minimum of 30 words)​

Answers

Answer:

“I think it’s about putting yourself in the students’ shoes and seeing how a first-time student, maybe someone who hasn’t even taken chemistry before, is looking at it.”

—Valerie Taraborelli, undergraduate chemistry student, University of Arizona

Sammy’s Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. Write a program that prompts the user for the number of minutes they rented the piece of sports equipment. Compute the rental cost as $40 per hour plus $1 for each additional minute. (Fix the logic error from Sammy's Rental program)



Display Sammy’s motto “Sammy’s makes it fun in the sun” surrounded by a border composed of repeated “Ss.” Save the file as SammysRental2.java

I just need lines of code and thats it

Answers

Answer:

import java.util.*;

public class SammysRentalPrice {

public static void main(String[] args) {

 System.out.println("SsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSs");

 System.out.println("Sammy's makes it fun in the sun");

 System.out.println("SsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSsSs");

 Scanner s=new Scanner(System.in);

 System.out.print("Enter the rented minutes:");  

 int min=s.nextInt();

 int hours=min/60;

 min=min%60;

 int tot_cost=(hours*40)+(min*1);

 

 System.out.println("total hours is: "+hours);

 System.out.println("total minutes is: "+min);

 System.out.println("total cost is: "+tot_cost);

 

}

}

Explanation: This is the code I made.

Who is the person that is on location reporting the news?.

Answers

Answer:

journalist

Explanation:

A journalist is an individual trained to collect/gather information in form of text, audio or pictures, processes them to a news-worthy form and disseminates it to the public. The act or process mainly done by the journalist is called journalism.

g bookmarks
5 4v
Next →
Illustrating Documents: Mastery Test
Type the correct answer in the box. Spell the word correctly.
What aspect should you consider before adding pictures to a document?
You should structure the
first before you search for a relevant picture.
Reset
Next

Answers

Before you search for a relevant picture you should first reset the structure.

When adding pictures to a document, you should first structure the reset and then look for a pertinent image. So, the correct option is A.

What are Pictures?

A photograph is an image made by light striking a photosensitive surface, either photographic film or an electronic image sensor like a CCD or CMOS chip. It is also referred to as a photo, image, or picture.

Any visual thing that has been edited or manipulated by a computer is referred to as an image, as is any imagined object produced by a computer. Everything captured by a camera, digital camera, or photocopier is a photo or photograph. A picture is a digitally produced sketch, painting, or other piece of art.

For above given example, when adding pictures to a document, we should first structure the reset and then look for a pertinent image.

Thus, the correct option is A.

Learn more about image, here:

https://brainly.com/question/2976060

#SPJ7

Lập trình đếm xem có bao nhiêu số chẵn , bao nhiêu số lẻ trong đoạn từ M đến N

Answers

I don’t understand your language pls translate it to English

Is all the information on the Internet truthful? Why or Why not.

Answers

All of the information on the internet is not truthful because some people just say things to say things and they don’t have accurate answers.

Answer:

No

Explanation:

Not all information that you read on the internet is true because some szites let other people post information that has no facts to back it up.

!!help please!!
Which type of data is shown here?​

Answers

It is string data. That means it is a sequence of characters

The data shown, "Monkey," is an example of a string data type. Therefore option B is correct.

In programming and computer science, a string is a sequence of characters (letters, numbers, symbols) that represent textual data.

Unlike numeric data types (A), which represent numbers, or Boolean data type (D), which represents true or false values, a string is used to store and manipulate text-based information.

Strings are commonly used to store names, sentences, messages, and other textual data in various programming languages and data structures.

In this case, "Monkey" is a text-based representation of an object or concept, making it a string data type.

Therefore option B string is correct.

Know more about programming languages:

https://brainly.com/question/23959041

#SPJ5

help please! I don’t know how to do this :’).

Answers

That is the base code right there though.

Another term for the plot structure of the hero journey

Answers

monomyth: In narratology and comparative mythology, the hero's journey, or the monomyth, is the common template of stories that involve a hero who goes on an adventure, is victorious in a decisive crisis, and comes home changed or transformed.

the process by which information gets into memory storage is

Answers

Answer:

That is the first step and it is called encoding process.

Other Questions
(WORTH MANY POINTS).......You dont have to find say whether theh are subject or pronoun.... Just pick the word that you think fits each question. Please answer each question. Thank you hamilton attended kings college, which is now known as what? 6.How did the Great Migration contribute to the development of the Harlem Renaissance?A. Many African Americans returned to the South after they achieved fame and success during the Harlem Renaissance.B. Many African Americans left the South to settle in the North, drawn by the cultural movement of the HarlemRenaissance.C. Many African Americans returned to the South to reclaim their history, leading to the development of the HarlemRenaissance.D. Many African Americans left the South to forge a new path for themselves in the North, leading to the development ofthe Harlem Renaissance. simplify 140/200 pleaseeeeee If you know Spanish please help I need help please? Humans, dimples are a dominant trait. Predict the percentage of children that will have dimples if one parent is heterozygous for dimples and the other parent does not have dimples.(1 point)0 percent100 percent50 percent25 percentIf a white horse and a black horse are bred together, what percentage of the offspring will be roan and express both colors in their hair?1 point)100 percent0 percent25 percent50 percent X-3 = square root of 4x Describe surface tension in a group of bonded water molecules that form a ""bead of water"" Do you agree with the supreme court's decision of the korematsu case? Why or why not? Which promise encouraged Anti-Federalists to ratify the Constitution? Another slave state would be added to the union. A bill of rights would be added to the Constitution Another branch of government would be added to the Constitution Another house would be added to the legislative branch 1. What was the problem the Separatist leaders faced?2. What was unusual about their solution to the problem?3. How does a body of laws such as the U. S. Constitution work to hold a nation together? Will makr brainliest!Hey could someone translate this to spanish and then add some casual mistakes in it? Thanks for helping me out. Hello, Mr. Robinson, how are you? you will have to turn right on Williams's field and go straight. Turn left on Stone Street and right into the left lane, then go straight until you see a Starbucks. To be exact you will have to enter the Starbucks driveway and turn around back where you will see a dirt road. Take that dirt road all the way until you see the fishing lake. Can someone pls help me A large multiplex movie house has many theaters. The largest theater has 48 rows. There are 13 seats in the first row. Each row has two seats more than the previous row. How many total seats are there in the theater? Use distributive property:7(x-12)-(6x-15) Which rebellion was caused by British mistreatment of American Indians in the Ohio Country and Great Lakes region?A. Pontiacs RebellionPontiacs Rebellion B. Stono RebellionStono RebellionC. Bacons RebellionBacons RebellionD. Powhatans Rebellion the graph of y=3x+2 please help!! i do NOT understand chemsitry at all this is me. i look amazing