Skip to main content

Newbie Help needed.

1 reply [Last post]
asroma123
Offline
Joined: 2008-10-25
Points: 0

Im relatively new with java and need some help with Switch statements. I know how to do a Switch statement but my problem is when I have to create a method with a switch statement. Im trying to return a string to the main but the compiler always brings up an error. This is the method :

public static "not sure which data type to enter here" determineMonthName(int month)
{
String unit = " ";

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

any help would be greatly appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tommyhit
Offline
Joined: 2008-10-25
Points: 0

Try this,

[code]
public class A {

public static String determineMonthName(int month) {
String unit = null;

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

public static void main(String[] args) {
System.out.println(determineMonthName(5));
}

}
[/code]

Message was edited by: tommyhit