-20

I receive a compiler error saying that the variable nameOfMonth might not have been initialized.

I know this is probably a simple fix but I am stuck and cannot seem to find an answer on Google.

public class Switch
{
    public static void main ( String [] args )
        {
        int month;
        String monthString, nameOfMonth;
        month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
        monthString = nameOfMonth;
        System.out.println(monthString);
        }

        protected static String nameOfMonth(int month)
        {
        String monthString;
        switch(month)
        {
                case 1: monthString = "January";
                        break;
                case 2: monthString = "Febuary";
                        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;
        }
        return monthString;
        }
}
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316

3 Answers3

2

nameOfMonth is not initialized before it is used on the right-hand side of the assignment:

String monthString, nameOfMonth;
month=ConsoleInput.readInt(
    "Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
monthString = nameOfMonth; // Correct compiler error here.

I suspect the code meant to invoke the same named method.

monthString = nameOfMonth(month);

and the nameOfMonth variable is unrequired.

hmjd
  • 120,187
  • 20
  • 207
  • 252
0

You want to call a method named nameOfMonth, you don't want to declare another string with that name. Change

    String monthString, nameOfMonth;
    month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
    monthString = nameOfMonth;
    System.out.println(monthString);

to

    String month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
    String monthString = nameOfMonth(month);
    System.out.println(monthString);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

As the error states, you try to use a variable (nameOfMonth) value before it is initialized.

You need to use:

month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
String monthString = nameOfMonth(month);

As you don't really use nameOfMonth variable, but nameOfMonth method.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61