Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a more efficient way to do this? | Month conversion
    primarykey
    data
    text
    <p>I was given a little task to complete where a user would be asked to input either a month or the numerical equivalent and it would return the numerical value of the entered month, or the month corresponding to the inputted numerical value. The constraints are as follows: - It must not contain a GUI of any kind - I have to use the BufferedReader for input - I have to use at least one Switch statement</p> <p>If anyone has any ideas, it would be greatly appreciated. My code so far is as follows:</p> <pre><code>/** * Month task * * @author Dan Foad * @version 0.01 */ import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Months { public static void main(String []args) throws IOException { int iInput; boolean isParseable; String szInput; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please type either the number of the month, or the month itself to convert."); System.out.print("&gt; "); szInput = br.readLine(); try { Integer.valueOf(szInput); isParseable = true; } catch(Exception e) { isParseable = false; } if (isParseable) { iInput = Integer.valueOf(szInput); System.out.println(numberToMonth(iInput)); } else { szInput = szInput.toLowerCase(); System.out.println(monthToNumber(szInput)); } return; } public static String numberToMonth(int iMonth) { String MonthReturn; switch(iMonth) { case (1): MonthReturn = "January"; break; case (2): MonthReturn = "February"; break; case (3): MonthReturn = "March"; break; case (4): MonthReturn = "April"; break; case (5): MonthReturn = "May"; break; case (6): MonthReturn = "June"; break; case (7): MonthReturn = "July"; break; case (8): MonthReturn = "August"; break; case (9): MonthReturn = "September"; break; case (10): MonthReturn = "October"; break; case (11): MonthReturn = "November"; break; case (12): MonthReturn = "December"; break; default: MonthReturn = "0"; break; } return MonthReturn; } public static int monthToNumber(String szMonth) { int MonthReturn; switch(szMonth) { case ("january"): MonthReturn = 1; break; case ("february"): MonthReturn = 2; break; case ("march"): MonthReturn = 3; break; case ("april"): MonthReturn = 4; break; case ("may"): MonthReturn = 5; break; case ("june"): MonthReturn = 6; break; case ("july"): MonthReturn = 7; break; case ("august"): MonthReturn = 8; break; case ("september"): MonthReturn = 9; break; case ("october"): MonthReturn = 10; break; case ("november"): MonthReturn = 11; break; case ("december"): MonthReturn = 12; break; default: MonthReturn = 0; break; } return MonthReturn; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload