patternjavaModerate
Printing the month name for the month number
Viewed 0 times
thenumberprintingnameformonth
Problem
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.
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
}Solution
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.
Once you have that, we can simply do:
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
then you can at least extract the repeated string part and formatting logic to a helper method:
And then rewrite the switch using that method:
But really, insisting on a
and goes against good practices.
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 onBut really, insisting on a
switch here is forced,and goes against good practices.
Code Snippets
String[] months = {"January", "February", "March", ..., "December"};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");
}private static void printMonth(int num, String name) {
System.out.printf("The name of month number %d is %s%n", num, name);
}switch (num)
{
case 1:
printMonth(num, "January");
break;
case 2:
printMonth(num, "February");
break;
// .... and so onContext
StackExchange Code Review Q#91615, answer score: 14
Revisions (0)
No revisions yet.