MrAwesome8
MrAwesome8

Reputation: 269

multiple switches for same case

I have this code:

switch(month){
    case 1:
        System.out.print("January");
            break;
case 2:
    System.out.print("February");
        break;
case 3:
    System.out.print("March");
        break;
case 4:
    System.out.print("April");
        break;
case 5:
    System.out.print("May");
        break;
case 6:
    System.out.print("June");
        break;
case 7:
    System.out.print("July");
        break;
case 8:
    System.out.print("August");
        break;
case 9:
    System.out.print("September");
        break;
case 10:
    System.out.print("October");
        break;
case 11:
    System.out.print("November");
        break;
case 12:
    System.out.print("December");
        break;
}

Okay so this code works 100% perfectly... for the int month. I have another int (avgMonth) which can only hold the same values(1-12) and I want to only have the same outputs (the months). How can I add avgMonth to this code without have to copy my whole switch and case? I tried use commas (month, avgMonth) and &&'s (month && avgMonth) and also +'s (month + avgMonth) but to no avail.

Upvotes: 1

Views: 145

Answers (5)

kavidop
kavidop

Reputation: 61

A simple Array could do the job, like

String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

then your month would be like

monthName = monthNames[month - 1];
avgMonthName = monthNames[avgmonth - 1];

Upvotes: 2

Boris the Spider
Boris the Spider

Reputation: 61128

First, delete that switch and pretend that you never wrote something so goddam ugly.

Now that you have a clean slate, create a enum:

public enum Month {

    JANUARY(1),
    FEBRUARY(2),
    //other months
    ;

    private static final Map<Integer, Month> LOOKUP;

    static {
        final Map<Integer, Month> lookup = new HashMap<>();
        for (final Month month : values()) {
            lookup.put(month.number, month);
        }
        LOOKUP = Collections.unmodifiableMap(lookup);
    }

    public static Month fromNumber(final int num) {
        final Month month = LOOKUP.get(num);
        if(month == null)
            throw new IllegalArgumentException("No month for number " + num);
        return month;
    }

    private final int number;

    private Month(final int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        final String name = name();
        return name.substring(0, 1) + name.substring(1).toLowerCase();
    }

}

And use it like so

final Month month = Month.fromNumber(someInt);

In order to print the month you can just to:

System.out.println(month);

And the toString method will covert it to sentence case.

Upvotes: 1

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 235984

Encapsulate the whole block of code in a method, and pass avgMonth and month as parameters. Like this:

public static void monthNumToName(int month) {
    // … same as before
}

Alternatively, you could simplify the code using a Map:

private static final Map<Integer, String> months;
static {
    months.put(1, "January");
    months.put(2, "February");
    months.put(3, "March");
    months.put(4, "April");
    months.put(5, "May");
    months.put(6, "June");
    months.put(7, "July");
    months.put(8, "August");
    months.put(9, "September");
    months.put(10, "October");
    months.put(11, "November");
    months.put(12, "December");
}

public static void monthNumToName(int month) {
    String name = months.get(month);
    if (name == null)
        throw new IllegalArgumentException("Invalid month number: " + month);
    System.out.print(name);
}

Or even simpler, just use an array, because we know beforehand that month numbers are restricted in the range 1-12:

private static final String[] months = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
};

public static void monthNumToName(int month) {
    if (month < 1 || month > 12)
        throw new IllegalArgumentException("Invalid month number: " + month);
    System.out.print(months[month-1]);
}

Anyway, when you need to print the name of the month do this:

monthNumToName(month);
monthNumToName(avgMonth);

Upvotes: 5

Alexis C.
Alexis C.

Reputation: 93842

You can use DateFormatSymbols, which have a getMonths() method.

Gets month strings. For example: "January", "February", etc.

public static String getMonth(int month){
    return new DateFormatSymbols(Locale.UK).getMonths()[month-1];       
}
  • I did month-1 because you want 1 to be mapped with January
  • You may add some checks for the bounds of the array returned by getMonths()

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533472

How about

private static String[] MONTHS = "none,January,February,March,April,May,June,July,August,September,October,November,December".split(",");
public static void printMonth(int month) {
     System.out.print(MONTHS[month]);
}

Upvotes: 1

Related Questions