Question

(InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user...

(InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user to enter an integer between 1 and 12 and then displays the months and its number of days corresponding to the integer entered. 1. Your program should display “Wrong number - Out of Bounds” if the user enters a wrong number by catching ArrayIndexOutOfBoundsException. 2. Also the program should display “Mismatch – not a number” if the user enters anything other than an integer by catching InputMismatchException. 3. The program should not terminate abnormally, i.e. until the user enters the right number. String[] months = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}; int[] dom = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: I have completed answer for your question. Please check and let me know if you have questions. I will revert back within 24 hours. Thanks for your patience.

Code:

import java.util.*;

class Array{
   public static void main(String[] args){
       Scanner sc = new Scanner(System.in);
  
       String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
       int[] dom = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

       System.out.println("enter an integer between 1 and 12? ");
       try{
           int mon = sc.nextInt();
           System.out.println("Month and its number of days are: "+ months[mon-1] + " " + dom[mon-1]);
       }
       catch(InputMismatchException ex){
           System.out.println("Mismatch – not a number");
       }
       catch(ArrayIndexOutOfBoundsException ex){
           System.out.println("Wrong number - Out of Bounds");
       }
   }
}


Code output screenshot:

Add a comment
Know the answer?
Add Answer to:
(InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • C++ Problem #1 Write a program that prompts user to enter an integer up to 1000...

    C++ Problem #1 Write a program that prompts user to enter an integer up to 1000 and displays back a message about the number of digits in this number and if number is odd or even. For example, if user enters 93 message back should be "93 has 2 digits and is odd".

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Java: Write a program that prompts the user to enter integers in the range 1 to...

    Java: Write a program that prompts the user to enter integers in the range 1 to 50 and counts the occurrences of each integer. The program should also prompt the user for the number of integers that will be entered. As an example, if the user enters 10 integers (10, 20, 10, 30, 40, 49, 20, 10, 25, 10), the program output would be: 10 occurs 4 times 20 occurs 2 times 25 occurs 1 time 30 occurs 1 time...

  • in java 3) Sum. Write a program that prompts the user to read two integers and...

    in java 3) Sum. Write a program that prompts the user to read two integers and displays their sum. Your program should prompt the user to read the number again if the input is incorrect.

  • Write a program that (in the main program) prompts the user for the (integer) model number...

    Write a program that (in the main program) prompts the user for the (integer) model number of their car. The main function should then call the function check defective which should • take in the model number of the car • check if the model number is 119, 179, 221, 780, or anything between 189 and 195, inclusive, which indicates the car is defective • print a message indicating whether or not the car is defective Note: this is should...

  • In c++ please .. Write a program that prompts the user to enter a four-digit integer...

    In c++ please .. Write a program that prompts the user to enter a four-digit integer and displays the number in reverse order.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT