-1

I understand that I need to initialize the variable daysInMonth, but I don't know how to since it's value at the point where I need it to determine the validity of the day is dependent on the user's input.

int daysInMonth; //number of days in month read in

part of code that determines daysInMonth

if(month == 1)
   daysInMonth = 31; 

code where I get the error

//User number of days in month to check to see if day is valid
if(day > 0 && day <= daysInMonth)
   dayValid = true;
else
   dayValid = false;
Katy
  • 1

5 Answers5

1

Think what daysInMonth should be for other values of month. Add an else, or may be else if. Make sure all possible values are covered, with initialization or an exception.

0

This initialization is not enough :

if(month == 1)
   daysInMonth = 31; 

Since daysInMonth will only be initialized if month==1.

The safest way is to initialize daysInMonth when you declare it, and change its value when necessary (based on the month).

int daysInMonth == 31;
if (month == 6)
   daysInMonth = 30;
...

Another option is to have a method that takes a month and returns the daysInMonth. Then you can initialize your variable by calling :

int daysInMonth = getDaysInMonth(month);
Eran
  • 387,369
  • 54
  • 702
  • 768
0

How about keeping it simple and doing it like this:

daysInMonth = 31; // Default value for the month
if(month == 2) {
   daysInMonth = 28;  // Change value if the month is febuary
} else if (month == 4 || month == 6 || month== 9 || month==11) {
   daysInMonth = 30; // Change value if the month should have 30 days
}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
0

You need to guarantee initialization of a variable.

int daysInMonth; //number of days in month read in
if(month == 1)
    daysInMonth = 31; 

This only initializes the variable if month is 1. If it isn't 1, it has still been uninitialized.

There are two approaches to solving this issue:

1. Initialize the variable at the beginning

By giving your value a default value, you guarantee it is not null.

int daysInMonth = 0; //number of days in month read in
if(month == 1)
    daysInMonth = 31; 

2. Initialize the variable at every branch

By intentionally not defining it, you can have the compiler tell you if you are missing any paths. This may be helpful for complex if/else statements where variables have specific values, and you don't want to accidentally default to 0 for a state where it should be -1, for example.

int daysInMonth; //number of days in month read in
if(month == 1)
    daysInMonth = 31; 
else if(month == 2)
    daysInMonth = 28; 
//...
else if(month == 12)
    daysInMonth = 31; 
else 
    daysInMonth = -1; 
Compass
  • 5,867
  • 4
  • 30
  • 42
0

There are many ways to solve this, but you can try this:

int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
daysInMonth = days[month-1]]; 

That's it! 2 lines of code is all you need.

If you need to check for leap year...you can try this:

int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int[] daysLeapYr = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (year % 4 == 0) //Leap year
    daysInMonth = daysLeapYr[month-1]];   //where month is 1-12 (Jan to Dec)
else
    daysInMonth = days[month-1]]; 

This way, you keep your codes clear with minimal if-statements.

If you are not checking for leap year, you don't even need any if-statements.

user3437460
  • 17,253
  • 15
  • 58
  • 106