1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
monitta
4 years ago
7

Write an application in which you declare an array of eight first names. Write a try block in which you prompt the user for an i

nteger 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 also should display the error message Subscript out of range.
Computers and Technology
1 answer:
meriva4 years ago
4 0

Answer:

import java.util.*;

public class Main

{

public static void main(String[] args) {

   

    String[] names = {"Ted", "Marshall","Lily", "Barney","Robin", "Stella", "Ranjit", "Wendy"};

 Scanner input= new Scanner(System.in);

 int i;

 try

 {

  System.out.print("Enter an integer(1 to 8) to display a name in that position: ");

  i = input.nextInt();

  i = i - 1;

  System.out.println(names[i]);

  input.close();

 }

 catch (ArrayIndexOutOfBoundsException e){

  System.out.println("Number is out of range!");

 }

   

}

}

Explanation:

Initialize an array containing 8 names

Inside the try block:

Ask the user for a number

Decrease the number by 1, so that it corresponds to the array index

Print the name in that position

Inside the catch block, define an ArrayIndexOutOfBoundsException that will print the error message, if the user enters a number that is not in the range

You might be interested in
let's imagine you're searching for hotels in searching chicago, what should you search for in order to get the most relevant res
n200080 [17]

Trivago, Trip advisor,

3 0
3 years ago
Which tab on the Ribbon contains the command to add a new, blank page to a publication?
aleksklad [387]

It's Insert. Insert > Page > Insert Page

4 0
3 years ago
Is windows 7 professional faster than home premium?
Olin [163]
Widows 7 is no longer supported for anti virus, I recommend a windows 10 license
8 0
3 years ago
The________ format is usually used to store data
Nikitich [7]
The answer is BCD; The BCD format is usually used to store data
5 0
3 years ago
A Function checkMe takes three parameters, a character and two integers. If the sum of the two integers is negative, and the cha
Paul [167]

Answer:

Check the explanation

Explanation:

Here is the program with function definition and two sample calls.

Code:

#include <iostream>

using namespace std;

//checkMe FUNCTION which takes values a, b and c

void checkMe(char &a, int &b, int &c)

{

//if sum of b and c is negative and a is 'n', b and c are set to 0, otherwise a is set to 'p'

if((b+c)<0 && a=='n')

{

b = 0;

c = 0;

}

else

{

a = 'p';

}

}

int main()

{

//first test case when else part is executed

char a = 'n';

int b = 5;

int c = 6;

 

checkMe(a, b, c);

 

cout<<a<<" "<<b<<" "<<c<<endl;

 

//second test case when if part is executed

a = 'n';

b = -4;

c = -5;

 

checkMe(a, b, c);

 

cout<<a<<" "<<b<<" "<<c<<endl;

return 0;

}

Kindly check the Output below:

7 0
3 years ago
Other questions:
  • #Write a function called string_finder. string_finder should #take two parameters: a target string and a search string. #The fun
    14·1 answer
  • \Read the sentence.
    12·1 answer
  • A bank uses a computer program during the night to tell if the alarm should ring. Sensors in the bank set the following Boolean
    5·1 answer
  • Which cell address indicates the intersection of the first row and the first column in worksheet?
    12·2 answers
  • What are the purposes of a good web page design?
    9·2 answers
  • What is the purpose of creating a primary key in a database
    14·1 answer
  • . A store offers a deposit of 6% on the cash price of $462. The deposit amount is?​
    6·1 answer
  • A small company with 100 computers has hired you to install a local area network. All of the users perform functions like email,
    9·1 answer
  • What is the definition of a flowchart? *
    13·1 answer
  • The development methodology where each part of a project is done in order after each other is called:
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!