简体   繁体   中英

Variable not initialized… but it was?

So the application I am working on at the moment requires 3 methods that return values to another class. The issue I'm having is with my second method that associates the users' input from the first method with the corresponding Named month. I continuously receive a "variable might not have been initialized" error from this set:

public String MonthName(int monthNumber){
    /*This method assigns a Name based on the users input and
    **returns the proper name of the corresponding month 
    ** @para: monthNameFin is the final name of the month
    **        rest should be self-explanatory              */

    String monthNameFin;

    if (monthNumber == 1)
        monthNameFin = "January";
    if (monthNumber == 2)
        monthNameFin = "February";
    if (monthNumber == 3)
        monthNameFin = "March";
    if (monthNumber == 4)
        monthNameFin = "April";
    if (monthNumber == 5)
        monthNameFin = "May";
    if (monthNumber == 6)
        monthNameFin = "June";
    if (monthNumber == 7)
        monthNameFin = "July";
    if (monthNumber == 8)
        monthNameFin = "August";
    if (monthNumber == 9)
        monthNameFin = "September";
    if (monthNumber == 10)
        monthNameFin = "October";
    if (monthNumber == 11)
        monthNameFin = "November";
    if (monthNumber == 12)
        monthNameFin = "December";

    return monthNameFin;
}

Any help is greatly appreciated!

You have to initialize the variable as :

String monthNameFin=null;

or

String monthNameFin="";

else if you have always an assignment of value you can skip the initialization, for example:

String monthNameFin;
   if (monthNumber == 1){
        monthNameFin = "January";
   }else{ 
        monthNameFin = "January";
   }

in this case there is no escape... to monthNumberFin will be in any case a value, while in your code it might happen that it never enters an if .. assuming it is for example montthumber=14

There is always the possibility that none of your cases matches, if monthNumber is less than or equal to 0, or if it's 13 or greater. This means that there is a possibility of the variable not being initialized.

Because numbers outside the range of 1-12 don't make sense, it's better to throw an IllegalArgumentException at the bottom if no case matches.

This example also includes changing all case assignment statements to return statements, so that there is a return or a throw for every case, even for the unmatched case.

  ...
  if (monthNumber == 12)
      return "December";

  // No case matched.
  throw new IllegalArgumentException("Bad month number: " + monthNumber);
}

The code that calls this method should catch this exception and handle it properly.

As the other answers stated, you have to initialized your variable.

But instead of all this if , I would use a DateFormatSymbols object.

public String MonthName(int monthNumber){
     if(monthNumber < 1 || monthNumber > 12)
           throw new IllegalArgumentException("Month must be in the range [1, 12]");
     DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.UK);
     return dfs.getMonths()[monthNumber-1];
}

Simpler way to write this that handles all integer cases:

public String monthName(int monthNumber) {
    if (monthNumber < 1 || monthNumber > 12) return null;
    String[] months = new String[] {"January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October", "November", "December"};
    return months[monthNumber];
}

Also,

(1) just initialize monthName at first, perhaps to

String month = monthName(monthNumber);

(2) you should probably begin method names with lowercase.

Should not enter the statement "IF" find an uninitialized variable.

Initializes monthNameFin.

Try this:

public String MonthName(int monthNumber){
    /*This method assigns a Name based on the users input and
    **returns the proper name of the corresponding month 
    ** @para: monthNameFin is the final name of the month
    **        rest should be self-explanatory              */

    String monthNameFin = ""; /* <----------- */

    if (monthNumber == 1)
        monthNameFin = "January";
    if (monthNumber == 2)
        monthNameFin = "February";
    if (monthNumber == 3)
        monthNameFin = "March";
    if (monthNumber == 4)
        monthNameFin = "April";
    if (monthNumber == 5)
        monthNameFin = "May";
    if (monthNumber == 6)
        monthNameFin = "June";
    if (monthNumber == 7)
        monthNameFin = "July";
    if (monthNumber == 8)
        monthNameFin = "August";
    if (monthNumber == 9)
        monthNameFin = "September";
    if (monthNumber == 10)
        monthNameFin = "October";
    if (monthNumber == 11)
        monthNameFin = "November";
    if (monthNumber == 12)
        monthNameFin = "December";

    return monthNameFin;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM