0

I have a problem that asks the user for a number from 1-12 and then the program returns the corresponding month. For example, if they user inputs the number "2" then the string "Feb" will be printed.

I know how to achieve this but I feel like there is a better, more efficient way of solving it. The way I was thinking of going about it is to just make 12 if statements for each number (1-12) and then have the corresponding month printed depending on the number.

Jon
  • 976
  • 8
  • 23
  • Is this homework? Look up `case` statements. – Miserable Variable Sep 05 '12 at 20:11
  • 1
    If you're in a class, you should probably tag this as homework. That being said, they are probably either trying to get you to learn about case statements on your own, or force you to use a big if-else chain so that when you learn case statements you can really appreciate what they do and how they are used. – Drake Clarris Sep 05 '12 at 20:28

8 Answers8

3

Use a Map keyed by the month number. The value would contain the month name.

Map<Integer, String> monthNames = new HashMap<Integer, String>();
monthNames.put(1, "January");
monthNames.put(2, "February");
...
int month = 3;
System.out.println(monthNames.get(month)); // prints March

Also consider using DateFormatSymbols to get the month names. See How can I convert an Integer to localized month name in Java?

Community
  • 1
  • 1
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
2

Use hashtables. Where each key points an element like your months. Maybe you can build your own hash function too. Could be O(1) access time rather than O(n).

 Hashtable numbers = new Hashtable();
 numbers.put("one", new Integer(1)); //String<-->Integer
 numbers.put("two", new Integer(2));
 numbers.put("three", new Integer(3));

  Integer n = (Integer)numbers.get("two");
 if (n != null) {
     System.out.println("two = " + n);
 }

 You can change the key and element(in this example they are String and Integer) like this:

 Hashtable<Integer, String> abc=new Hashtable<Integer, String>();
    abc.put(new Integer(4), "hello");

Hashtables accept object for both key and item so you can use any class that extends Object. Very flexible. But, playing with object could decrease performance a bit, since you are not saying about anything like a "benchmark" , this could be your friend.

 __________________________________________________________
| You can use Integer.valueOf() (available since Java 1.5) |
| instead of new Integer   Credit to "Steve Kuo"           |
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
2

Using a switch statement is a way. Although there are more elegant ways using different data structures, but given that you are in an introductory course, it would be harder to understand data structures.

Sednus
  • 2,095
  • 1
  • 18
  • 35
1

you can simply use an array to achieve this

arr = {"january", "february"
// etc
}
gefei
  • 18,922
  • 9
  • 50
  • 67
1

You can use a switch statement since you're not using any structures, or else I'd recommend a map, though it will very much feel like a bunch of if statements at first.

int monthNumber;
    String month;
    switch(monthNumber){
        case 1: month = "January";
                break;
        case 2: ...
    }
Jeff LaJoie
  • 1,725
  • 2
  • 17
  • 27
1

Many solutions:

  • use a Map mapping the numbers 1, 2, 3 to "Jan", "Feb", "Mar", etc.
  • use an array of Strings, but remember that array indices start with 0: String[] months = new String[] {"0 is no month", "Jan", "Feb", "Mar", ... };
  • use a switch/case statement

If you need this only once, the switch/case would IMHO be the cleanest.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Use a map like that Map months. Then months.get(i) to get MonthName.

gregory561
  • 14,866
  • 2
  • 23
  • 25
0

You can use the switch statement, or you can use some data structure instead, like a map, an array, a list or so.

Switch Example 1:

public String getMonth(int month)
{
    switch (month)
    {
        case 1: return "January";
        case 2: return "February";
        case 3: return "March";
        case 4: return "April";
        case 5: return "May";
        case 6: return "June";
        case 7: return "July";
        case 8: return "August";
        case 9: return "September";
        case 10: return "October";
        case 11: return "November";
        case 12: return "December";
    }
}

Switch Example 2:

public String getCondition(int month)
{
    String message = "";
    switch (month)
    {
        case 1: message = "January";
            break;
        case 2: message = "February";
            break;
        case 3: message = "March";
            break;
        case 4: message = "April";
            break;
        case 5: message = "May";
            break;
        case 6: message = "June";
            break;
        case 7: message = "July";
            break;
        case 8: message = "August";
            break;
        case 9: message = "September";
            break;
        case 10: message = "October";
            break;
        case 11: message = "November";
            break;
        case 12: message = "December";
            break;
        default: message = "Invalid month";
    }
}

Try these examples out.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175