Home > Mobile >  Print month name using switch statement until valid input given
Print month name using switch statement until valid input given

Time:11-19

Using the Switch Statement, create a program that will ask the user to input number from 1 - 12, then each number corresponds a month in the calendar. If the number is not on the range display "The value is not on the calendar." Then it will ask the user if they want to try again a number or it will close the program. If the user input Y for Yes then it will execute again the program. If the user chose N for No it will automatically terminate the program and it will display System is Terminated.

import java.util.Scanner;

public class calendar {

      public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            System.out.print("Enter month's number: ");

            int monthNumber;

            monthNumber = in.nextInt();

            switch (monthNumber) {

            case 1:

                  System.out.println("January");
                          
                  break;

            case 2:

                  System.out.println("February");

                  break;

            case 3:

                  System.out.println("March");

                  break;

            case 4:

                  System.out.println("April");

                  break;

            case 5:

                  System.out.println("May");

                  break;

            case 6:

                  System.out.println("June");

                  break;

            case 7:

                  System.out.println("July");

                  break;

            case 8:

                  System.out.println("August");

                  break;

            case 9:

                  System.out.println("September");

                  break;

            case 10:

                  System.out.println("October");

                  break;

            case 11:

                  System.out.println("November");

                  break;

            case 12:

                  System.out.println("December");

                  break;

            default:

                  System.out.println("Invalid month.");

                  break;

            }

      }

}

CodePudding user response:

First, follow your instructions - If the number is not on the range display "The value is not on the calendar.", not "Invalid month".

You can use a regular if statement for that, rather than a switch.

System.out.print("Enter month's number: ");
try {
    int monthNumber = Integer.parseInt(in.nextLine()); 
    if (monthNumber < 1 || monthNumber > 12) {
        throw new RuntimeException("out of bounds");
    } 
    System.out.println(monthName(monthNumber)); // extract your original logic to a method
} catch (Exception e) {
    System.out.println("The value is not on the calendar.");
} 

ask the user if they want to try again

Doesn't look like you tried this, but wrap the above in a while loop like so

String again = "Y";
while ("Y".equals(again)) {
    System.out.print("Enter month's number: ");
    try {... 
} 

In the catch body, add

System.out.println("Try again? (Y/N)"); 
again = in.nextLine().strip();

and it will display System is Terminated

Again, follow the instructions to print this after the while loop

CodePudding user response:

But doesn't it work? What's your problem?

  • Related