GoombaK1ller11
GoombaK1ller11

Reputation: 3

Error "this method must return a result of type int"?

I have this code below and it keep telling me that thanksgiving() must return a result type of int. I have casted all the results just to make sure, but nothing seems to be working! Why am I getting this error?

public class Thanksgiving
{
private static final int YEAR = 2000;  // first valid year for this method
private static final int NOV1 = 3;     // 2000/11/01 falls on a Wednesday
private static final int THURS = 4; // (Sun = 0, Mon = 1, ..., Sat = 6)

// Precondition: year > 1999
public static int thanksgiving(int year)
{
  int day = firstOfMonth( year );
  if ( day == THURS ) 
  {
    return (int) 22;
  }
  if ( day > THURS )
  {
    return (int) 29 - ( day - THURS );
  }
  if ( day < THURS )
  {
    return (int) 22 + ( THURS + day );
  }
}
public static int firstOfMonth(int year)
{
  int raw = year - 2000;
  int day = NOV1;
  for(int i = 0; i < raw; i++ )
  {
    if( i % 4 == 0 )
    {
      day = day + 2;
    }
    else
    {
      day++;
    }
  }
  return day % 7;
}

public static void main(String[] args)
{

    for(int year = 2000; year <= 2100; year++)
    {
        System.out.print("T'giving " + year + " is Nov " + thanksgiving(year) + "; ");
        if (year % 3 == 1)
        {
            System.out.println();
        }
    }
}

}

Upvotes: 0

Views: 359

Answers (6)

Greg
Greg

Reputation: 1243

The Java compiler is too stupid to realize that in every situation, one of your conditions will be satisfied. It sees the if statements and thinks that it is possible that none of them will be satisfied. You can add a return at the end of the method that indicates an error case, you can throw an exception, or you can assert false.

Upvotes: 0

Please
Please

Reputation: 296

All the returns are in if statements, in this case, compiler sees that there might be a situation in which the function won't return anything. You need to add one return outside any ifs or in an else statement.

Upvotes: 0

David.Jones
David.Jones

Reputation: 1411

The problem is that all of your return statements are within an if statement. If, for some reason, day != Thursday, not < thursday, and not > thursday (I know this is impossible), then nothing will be returned. Change your code to have a default return value:

public static int thanksgiving(int year)
{
  int day = firstOfMonth( year );
  if ( day == THURS ) 
  {
    return (int) 22;
  }
  else if ( day > THURS )
  {
    return (int) 29 - ( day - THURS );
  }
  else 
  {
    return (int) 22 + ( THURS + day );
  }
}

Upvotes: 1

Pradeep Simha
Pradeep Simha

Reputation: 18123

You have 3 if statements inside thanksgiving() method. What if none of the condition gets satisified? What would return in that case? So compiler is complaining.

Upvotes: 0

Andrey Pushin
Andrey Pushin

Reputation: 196

You need something like this

public static int thanksgiving(int year)
{
    int day = firstOfMonth( year );
    if ( day == THURS )
    {
        return (int) 22;
    } else if ( day > THURS )
    {
        return (int) 29 - ( day - THURS );
    } else {
        return (int) 22 + ( THURS + day );
    }
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

In this code:

public static int thanksgiving(int year)
{
  int day = firstOfMonth( year );
  if ( day == THURS ) 
  {
    return (int) 22;
  }
  if ( day > THURS )
  {
    return (int) 29 - ( day - THURS );
  }
  if ( day < THURS )
  {
    return (int) 22 + ( THURS + day );
  }
}

You're making your return statement in several if blocks. What if none of them are true? The compiler will not allow this, and you should either return a default value at the bottom or throw an exception. Or else make some of the if statements else - if with a last else:

public static int thanksgiving(int year){
  int day = firstOfMonth( year );
  if ( day == THURS ) {
    return (22;
  } else if ( day > THURS ) {
    return 29 - ( day - THURS );
  } else { // else without the if
    // we know that ( day < THURS )
    return 22 + ( THURS + day );
  }
}

Also:

  • There is no need to cast an int into an int.
  • An unrelated issue is that your code uses a lot of "magic" numbers and you will want to avoid using them.

Upvotes: 1

Related Questions