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.
Use a
Mapkeyed by the month number. The value would contain the month name.Also consider using
DateFormatSymbolsto get the month names. See How can I convert an Integer to localized month name in Java?