Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following Java code uses Scanner to obtain a number between 0 and 6 (inclusi

ID: 3762025 • Letter: T

Question

The following Java code uses Scanner to obtain a number between 0 and 6 (inclusive) from the user and save the number in variable. The code has two limitations:

It does not handle the situation when the user input is not a number. If the input is not a number it throws NumerFormaException exception and stops. Modify the code to fix the above problem: when the user input is not a number, the program prints a “not a number” message and asks the user to enter another number. Use try- catch.

It does not handle a situation when the input is out of array size. It will throw ArrayIndexOutOfBoundsException for invalid array inputs. Fix the problem so when the number is out of range the program prints entered index is out of bounds and lets user input another number. Use try- catch.
Note: Program successfully terminates only when the user inputs ###.

import java.util.Scanner;

public class ArrayExceptionTest {

      

       public static String[] weekdays = {"Monday", "Tuesday", "Wednesday",

                      "Thursday", "Friday", "Saturday", "Sunday"};

       public static void main(String[] args) {

              Scanner userInput = new Scanner(System.in);

              System.out.println("Enter the index number to print the week day name.

                                         Enter ### to exit: ");

              int index=0;

              while(true){

                     String input = userInput.nextLine();

                     if(input.equals("###")){

                           System.out.println("The program is finished!");

                           break;

                     }

                     index = Integer.parseInt(input);

                     String dayName = weekdays[index];

                     System.out.println("The week day name is:"+dayName);

                     System.out.println("Enter the index number to print the week day

                                   name. Enter ### to exit: ");

              }

       }

}

The code above can be found in given java file ArrayExceptionTest.java. Modify ArrayExceptionTest.java and turn it in. Your output should look exactly like the sample output below.

Enter the index number to print the week day name. Enter ### to exit:

fewfacdsa

The user input is not a number

Enter the index number to print the week day name. Enter ### to exit:

0

The week day name is:Monday

Enter the index number to print the week day name. Enter ### to exit:

5

The week day name is:Saturday

Enter the index number to print the week day name. Enter ### to exit:

-1

The entered index is out of bounds

Enter the index number to print the week day name. Enter ### to exit:

98

The entered index is out of bounds

Enter the index number to print the week day name. Enter ### to exit:

###

The program is finished!

Explanation / Answer

import java.util.Scanner;

public class ArrayExceptionTest {

public static String[] weekdays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

   public static void main(String[] args) {

       Scanner userInput = new Scanner(System.in);

       System.out.println("Enter the index number to print the week day name.Enter ### to exit: ");

       int index = 0;

       while (true) {

           try {

               String input = userInput.nextLine();

               if (input.equals("###")) {

                   System.out.println("The program is finished!");

                   break;

               }

               index = Integer.parseInt(input);

               String dayName = weekdays[index];

               System.out.println("The week day name is:" + dayName);

               System.out.println("Enter the index number to print the week dayname. Enter ### to exit: ");

           } catch (NumberFormatException e) {

               System.out.println("The user input is not a number");

               System.out.println("Enter the index number to print the week day name. Enter ### to exit:");

           } catch (ArrayIndexOutOfBoundsException e) {

               System.out.println("The entered index is out of bounds");

               System.out.println("Enter the index number to print the week day name. Enter ### to exit:");

               continue;

           }

       }

   }

}