4
\$\begingroup\$

This program asks a user to input any number equal to or between 1-12. It then converts the number to a message that will be printed (Copy the program to see it yourself). Is there a way to make the code shorter by still using the switch statement?

This is homework, so using a switch is a requirement.

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");
        }
    } // main method
}
\$\endgroup\$
  • \$\begingroup\$ I wish the downvoter had left a comment, but I'll take a guess and say it's likely to be because you've not explained why keeping the switch is a requirement. Is this homework? \$\endgroup\$ – RubberDuck May 24 '15 at 11:06
  • \$\begingroup\$ Yes it is an assignment \$\endgroup\$ – Amber May 24 '15 at 18:29
14
\$\begingroup\$

As you have probably noticed, there is a lot of repetition which we can take advantage of. We can declare an array containing all the string representations of the months like so.

String[] months = {"January", "February", "March", ..., "December"};

Once you have that, we can simply do:

if (num >= 1 && num <= 12) {
    System.out.format("The name of month number %d is %s\n", num, months[num-1]);
} else {
    System.out.println("You have entered an invalid number");
}

Edit: sorry, just saw the bit about "by still using the switch statement". I'll still leave this here though because I think it's a better solution than a switch statement.

If you really insist on using the switch (but you shouldn't), then you can at least extract the repeated string part and formatting logic to a helper method:

private static void printMonth(int num, String name) {
    System.out.printf("The name of month number %d is %s%n", num, name);
}

And then rewrite the switch using that method:

switch (num)
{
case 1:
    printMonth(num, "January");
    break;
case 2:
    printMonth(num, "February");
    break;
// .... and so on

But really, insisting on a switch here is forced, and goes against good practices.

\$\endgroup\$
  • 1
    \$\begingroup\$ Your answer was good. I added to it a bit, so that you can remove the apologetic edit ;-) \$\endgroup\$ – janos May 24 '15 at 7:45
  • \$\begingroup\$ This is exactly what look up tables are for. I like your original answer. ++ \$\endgroup\$ – RubberDuck May 24 '15 at 10:58
1
\$\begingroup\$

Month enum:

You could simply use the Month enum. This enum is defined as part of the new java.time framework built into Java 8 and later.

You can use this to easily get the name of the month:

int monthNumber = 5;
Month.of(monthNumber).name();

Output will be:

MAY

\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! Could you link to somewhere in the docs to demonstrate this? It's an unusual construction, but it looks like you're creating a Month class then calling two methods on it to get a string? Some elaboration would be very helpful. \$\endgroup\$ – SuperBiasedMan Oct 5 '15 at 9:02
0
\$\begingroup\$

First of all I would add a loop around the input to make sure that a proper month number is entered. This could also be extended with exception handling if that has been covered, as the user might input text.

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

    if (monthNumber < 1) && (monthNumber > 12) {
        System.out.println("You have entered an invalid month number");
    } else {
        break; // Number is a valid month number
    }
}

Secondly, my take on converting the monthNumber into a name when being constrained to using a switch would be something like the following:

switch(monthNumber): {
  case  1: monthName = 'January'; break;
  case  2: monthName = 'February'; break;
  ...
  case 12: monthName = 'December'; break;
}

System.out.printf("The name of month number %d is %s\n", monthNumber, monthName);

This way the code is concise and precise. The error handling is close to the actual input, and the "business logic" is tightly coupled in the switch, before a final presentation statement at the end.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.