user1471980
user1471980

Reputation: 10636

unable to find problems with this piece of code

I have this piece of code, I think I have all the braces covered. Still getting braces not closed error. When I put more braces, then gives me something else. Can you anybody tell me what I am doing wrong here?

    public static boolean isValid(int day, int month, int year)
    {
        if (year < 1900)
         {
            return false;
        }
        else {
            if (month>0 && month<13)
                {
                    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) 
                        {
                            return day > 0 && day <=31;

                        }
                    else if (month==4 || month==6 || month==9 || month==11)
                        {
                            return day>0 && day<=30;
                         }
                    else if (month==2) 
                         {      
                            if (isLeap(year))
                                 {
                                    //(d= 29);
                                    return day>0 && day <=29;
                                 }

                                else {
                                    return day>0 && day<= 28;
                                 }
                         }
            }

        }
        }   
}

Upvotes: 0

Views: 87

Answers (3)

math487
math487

Reputation: 253

if you're using eclipse use ctrl shift f to balance braces easier to read and see if you miss one or need one

Upvotes: 0

Salih Erikci
Salih Erikci

Reputation: 5087

Because of your if-else conditions it may not be able to return a boolean value.That is the problem.Check your code's logic.

Upvotes: 1

thedajaw
thedajaw

Reputation: 168

I don't think you are missing any braces but you are not returning values for every possible execution path.

if you put a return false at the bottom of the function it will compile.

Little tip, to save the reader all of those nested ifs and braces:

if(day>0 && day<= 28){
  return true;
else{
  return false; 
}

you can do this:

return day > 0 && day <= 28;

Upvotes: 0

Related Questions