Hey guys I am supposed to write a Java code that is able to print the month name to the corresponding month number and the other way around... if the input is invalid the programm should just print an error message. Other requirements are the switch statement and a static class method boolean to determine the type of input. Unfortunately I have no idea how to determine the data type and how to use the switch statement for strings... Thats what I've done so far, it works for the first part:
import java.util.Scanner;
public class MonthMapping{
public static boolean isMonthNumber(String month) {
int monthnumber = Integer.parseInt(month);
if((monthnumber >= 1) && (monthnumber <= 12)) {
return true;
}
else {
return false;
}
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter month: ");
int month = sc.nextInt();
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
Can anybody give me a solution for the second part? Or at least some hints how to do it?