question archive Tom wants to store the price details of the products that he purchased from the departmental store

Tom wants to store the price details of the products that he purchased from the departmental store

Subject:Computer SciencePrice:3.87 Bought7

Tom wants to store the price details of the products that he purchased from the departmental store. Help him by using the concept of Arrays.

Create public class ArrayException with a method getPriceDetails as :

public String getPriceDetails() - This method should do the following

Get the size of an array as input and then get the elements of the array(all elements are int) as input.

Next, user should provide the index of the array. This method should return the element at that index as "The array element is "+<that value>

 

This program may generate ArrayIndexOutOfBoundsException / InputMismatchException

In case of ArrayIndexOutOfBoundsException, the function should return "Array index is out of range".

When providing the input, if the input is not an integer, it will generate InputMismatchException. In this case the function should return "Input was not in the correct format".

Use exception handling mechanism to handle the exception. Use separate catch block for handling each exception. In the catch block, return the appropriate message.

Write main method and test the above function.

Sample Input 1:

Enter the number of elements in the array

5

Enter the price details

50

80

60

70

40

Enter the index of the array element you want to access

1

Sample Output 1:

The array element is 80

Sample Input 2:

Enter the number of elements in the array

2

Enter the price details

50

80

Enter the index of the array element you want to access

9

Sample Output 2:

Array index is out of range

Sample Input 3:

Enter the number of elements in the array

2

Enter the price details

30

j

Sample Output 3:

Input was not in the correct format

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

Java code

 

import java.util.*;

public class ArrayException {

  public static void main(String[] args)  {

ArrayException m =new ArrayException();

String s = m.getPriceDetails();

 System.out.println(s);

  }

public String getPriceDetails() {

String result = "";

Scanner  input = new Scanner(System.in);

 try{

 System.out.println("Enter the number of elements in the array");

int size = input.nextInt();

System.out.println("Enter the price details");

int list[] = new int[size];

for (int i = 0 ; i < size ; i++){

    list[i] = input.nextInt();
    }

System.out.println("Enter the index of the array element you want to access");

int index  = input.nextInt();

    result = "The array element is "+ list[index]  ;

        }catch (InputMismatchException exception) {

         result = "Input was not in the correct format" ;

         }catch (ArrayIndexOutOfBoundsException  exception) {

                     result = "Array index is out of range" ;

             }
 return result ;
}


}

Step-by-step explanation

steps of solution

 

created public String getPriceDetails() method

public String getPriceDetails() {

 

defined try with multi catch

 try{
        }catch (InputMismatchException exception) {

        

         }catch (ArrayIndexOutOfBoundsException  exception) {

 

ask user to enter the size of array and ask again to enter the values for array then enter the index he wants to show

 System.out.println("Enter the number of elements in the array");

int size = input.nextInt();

System.out.println("Enter the price details");

int list[] = new int[size];

for (int i = 0 ; i < size ; i++){

    list[i] = input.nextInt();
    }


System.out.println("Enter the index of the array element you want to access");

int index  = input.nextInt();



    result = "The array element is "+ list[index]  ;

 

if user entered index not in the range then print

         }catch (ArrayIndexOutOfBoundsException  exception) {

                     result = "Array index is out of range" ;

 

if user entered not integer value then print

  }catch (InputMismatchException exception) {

         result = "Input was not in the correct format" ;

 

calling method in main

  public static void main(String[] args)  {

ArrayException m =new ArrayException();

String s = m.getPriceDetails();

 System.out.println(s);