Make a program that prints the name of the month according to the corresponding number shorter

This program prompts the user to enter any number, equal to or from 1 to 12. It then converts the number into a message to be printed (copy the program to see it yourself). Is there a way to make the code shorter?

import javax.swing.JOptionPane;

public class NumOfMonth {

public static void main(String[] args) {



    int num = Integer.parseInt (JOptionPane.showInputDialog ("Enter any number equal to or between 1-12 to display the month"));

    switch (num)
    {
    case 1:
        System.out.println ("The name of month number 1 is January");
        break;
    case 2:
        System.out.println ("The name of month number 2 is February");
        break;
    case 3:
        System.out.println ("The name of month number 3 is March");
        break;
    case 4:
        System.out.println ("The name of month number 4 is April");
        break;
    case 5:
        System.out.println ("The name of month number 5 is May");
        break;
    case 6:
        System.out.println ("The name of month number 6 is June");
        break;
    case 7:
        System.out.println ("The name of month number 7 is July");
        break;
    case 8:
        System.out.println ("The name of month number 8 is August");
        break;
    case 9:
        System.out.println ("The name of month number 9 is September");
        break;
    case 10:
        System.out.println ("The name of month number 10 is October");
        break;
    case 11:
        System.out.println ("The name of month number 11 is November");
        break;
    case 12:
        System.out.println ("The name of month number 12 is December");
        break;
        default:
            System.out.println ("You have entered an invalid number");
        }
    }
} 

      

+3


source to share


2 answers


Yes, using DateFormatSymbols

:

return new DateFormatSymbols().getMonths()[num - 1];

      



getMonths

returns an array of months.

I highly recommend that you check the bounds before accessing the array.

+11


source


import javax.swing.JOptionPane;

public class NewClass {

    public static void main(String[] args) {

        String[] months = new String[]{
            "",
            "JAN",
            "FEB",
            "MAR",
            "APR",
            "MAY",
            "JUN",
            "JUL",
            "AUG",
            "SEP",
            "OCT",
            "NOV",
            "DEC"
        };

        int num = Integer.parseInt(JOptionPane.showInputDialog("Enter any number equal to or between 1-12 to display the month"));

        if (num >= 1 && num <= 12) {
            System.out.println("Name of month is " + months[num]);
        } else {
            System.out.println("INVALID ENTRY");
        }
    }
}

      



+3


source







All Articles