-1

For my Comp Sci class I have to make a program that finds the number of days in the given month(1-12), do ya'll know why I'm getting the error "variable days might not have been initialized" when trying to return the int "days" from the switch? Here's the code:

public static int getNumberofDays(int month,int year)
   {
      // Imports the required Scanner
      Scanner kbd = new Scanner(System.in);

      final String month;
      final int days;
      switch (month) {
         case 1:  days = 31;
            break;
         case 2:  if ((year % 4 == 0) && year % 100 != 0)
                  {
                     days = 29;
                  }
                  else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
                  {
                     days = 29;
                  }
                  else
                  {
                     days = 28;
                  }
            break;
         case 3:  days = 31;
            break;
         case 4:  days = 30;
            break;
         case 5:  days = 30;
            break;
         case 6:  days = 31;
            break;
         case 7:  days = 31;
            break;
         case 8:  days = 31;
            break;
         case 9:  days = 30;
            break;
         case 10: days = 31;
            break;
         case 11: days = 30;
            break;
         case 12: days = 31;
            break;
         default: month = "invalid";
            break;
      }
      return days;
   }
Cœur
  • 37,241
  • 25
  • 195
  • 267
MooTorial
  • 45
  • 1
  • 5

4 Answers4

7

why I'm getting the error "variable days might not have been initialized"

Because:

final int days;

should be:

int days = 0;

Why?

  • Variables in methods should be initialized
  • Final variables cannot be modified.

Other errors:

  • You don't cover all possible values of month in switch
  • you define a String month, shadowing the parameter int month
  • The Scanner is unused
  • many more... XD

My solution using your way

Also, if you don't break a case block next one will be executed, that means you can do:

public static int getNumberofDays(int month,int year) {
    switch (month) {
       case 1:  
       case 3:  
       case 6:  
       case 7:  
       case 8:
       case 10: 
       case 12: 
             days = 31;
          break;
       case 4:
       case 5:
       case 9:
       case 11: 
             days = 30;
          break;
       case 2:  
          if ((year % 4 == 0) && year % 100 != 0)
          {
             days = 29;
          }
          else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
          {
             days = 29;
          }
          else
          {
             days = 28;
          }
          break;
       default: days = -1;
          break;
    }
    return days;
}

My solution: Calendar!:

public static int getNumberofDays(int month,int year)
{
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.YEAR, year);
    return cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
}

Neil solution: Java-8

public static int getNumberofDays(int month,int year)
{
    YearMonth yearMonthObject = YearMonth.of(year, month); 
    return yearMonthObject.lengthOfMonth();
}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

Not all possible paths through your code assign a value to the days variable. Precisely the default block does not.

default: month = "invalid";
           break;

If you change it to

default: month = "invalid";
         days=0
           break;

The warning disappears.

My other questions are:

  • Why do you define a String month, thus shadowing the parameter month?
  • Whats the Scanner for?
  • Why do you assign invalid to month in the default block, you never return it.
Eashi
  • 347
  • 1
  • 9
0
  1. final String month; is invalid since month is supplied as a parameter to the function.

  2. You don't initialise days on all control paths: The default block in your switch doesn't do it. This is a nice feature of Java as it helps you achieve program stability. There's nothing better than the compiler telling you what to do. Don't force the issue by writing final int days = 0;; instead assign a value to it in the default block.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Especially now I've read the program more carefully – Bathsheba Feb 18 '16 at 15:36
  • It's worth mentioning that Java 8 now allows you to do simply this to get days of month: `YearMonth yearMonthObject = YearMonth.of(1999, 2); int daysInMonth = yearMonthObject.lengthOfMonth();` – Neil Feb 18 '16 at 15:40
  • Indeed. I'm assuming (perhaps wildly) that this was a school exercise. – Bathsheba Feb 18 '16 at 15:41
  • That seems accurate, but god forbid that a professional programmer stumble onto this page and copy that huge switch statement into his program. – Neil Feb 18 '16 at 15:42
-2

Add days = 0; to your default case, as such:

default: 
    month = "invalid";
    days = 0;
        break;

You have to give days a value somewhere in the code path before you try to return it. You should also replace final int days; with int days;. Also, you cannot have final String month because month is passed as a parameter to the function.. just remove the redeclaration of month and you're golden.

mike
  • 455
  • 2
  • 6