I have this method to see whether not the year is a leap year or not.
heres the code

i get the error missing return statement at the end.

What can i do , i can't add return true or return false, becuase that screws it up.

public boolean isLeapYear()
{ if(years % 4 == 0)
    { if(years % 100 == 0)
        { if( years % 400 == 0)
                return true;
            else
                return false;
        }   

    }   
  else 
    return false;   
}

Dani AI

Generated

The compiler error means at least one possible path through the method can exit without returning a boolean. As pointed out, the branch where years % 4 == 0 and years % 100 != 0 never reaches a return, so the compiler reports "missing return statement." Two clean fixes follow.

A concise, idiomatic implementation (and safe for unit tests) is to compute and return the boolean expression directly:

public boolean isLeapYear(int year) {
    return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

This implements the Gregorian rule: divisible by 4, except when divisible by 100 unless also divisible by 400. Parentheses are important to ensure the intended grouping of && and ||.

If keeping a boolean flag like in ’s suggestion, be sure the flag is always initialized and all assignments end with semicolons (the posted snippet is missing those). Example test cases to verify behavior: 1996 -> true, 1900 -> false, 2000 -> true, 2001 -> false. Also note the calendar caveat: the Gregorian leap-year rule applies starting with the 1582 reform; older historical dates may use the Julian rule. Finally, if years in the original post is intended to be a field, consider switching to a parameter named year to make the method easier to test.

Recommended Answers

All 5 Replies

Here is your code fully parenthesized and inside code tags.

public boolean isLeapYear() {
      if (years % 4 == 0) {
         if (years % 100 == 0) {
            if ( years % 400 == 0) {
               return true;
            } else {
               return false;
            }
         }  // what should be returned here if (years % 100 != 0) ?
      } else {
         return false;
      }
   }

This is what the compiler is complaining about.

: Line No 9;
return true;// if it is not divisible by 100 and divisible by 4 , then its a leap year.

Hope it works =D

public boolean isLeapYear()
    { 
        boolean leap = false;
        if(years % 4 == 0)
        {
            if(years % 100 == 0)
            {
                if(years % 400 == 0)
                {
                    leap = true
                }
            }else{
                leap = true;
            }
        }
        return leap;    
    }

Hope this helps =]]

public boolean isLeapYear()
		{ 
			boolean leap = false;
			if(years % 4 == 0)
			{
				if(years % 100 == 0)
				{
					if(years % 400 == 0)
					{
						leap = true
					}
				}else{
					leap = true;
				}
			}
			return leap;	
		}

To new_programmer and Tabone3:

My response was to vikas.kethineed, so my question was intended to be answered by the original poster.

I know you were trying to help, but if you read my message more carefully you might understand that I was just explaining why the compiler complained about a missing return statement, and vikas.kethineed needs to come up with the answer alone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.