Error *cannot return a value from method whose result type is void*

C

Charles

I am creating a program, but keep receiving the same error what I am
trying to do is have it output a calendar. I have it where it outputs
the month name and year inputted, and the days of the week. I can not
however get it to return a value. It says "cannot return a value from
method whose result type is void". If I change it to anything else I
get the error "missing return statement". I have already been to:
http://java.sun.com/docs/books/tutorial/java/javaOO/methoddecl.html#returnvalue

That site was no help at all. Here it the part of the program I am
trying to get to work.

/**********************************************
* Get number of days in a month.
***********************************************/

// THIS IS THE PROBLEM AREA!!!
public void numOfDaysInMonth()
//
{
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12))
return 31;

if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
year = isLeapYear;

if (month == 2)
if(isLeapYear == year)

return 29;
else
return 28;

for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
monthly[row][column] = column + row;
}

for (int i = 1800; i < year; i++)
{
if (isLeapYear == isLeapYear)
total = total + 366;
else
total = total + 365;
}

for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
System.out.print(" " + monthly[row][column]+ " ");
System.out.println();
}
}
}
 
E

Eric Sosman

Charles said:
I am creating a program, but keep receiving the same error what I am
trying to do is have it output a calendar. I have it where it outputs
the month name and year inputted, and the days of the week. I can not
however get it to return a value. It says "cannot return a value from
method whose result type is void". If I change it to anything else I
get the error "missing return statement". I have already been to:
http://java.sun.com/docs/books/tutorial/java/javaOO/methoddecl.html#returnvalue

This is what might be called an "elementary Java language
question" or (less politely) a "newbie question." There's
nothing wrong with such questions, but they belong in the
comp.lang.java.help newsgroup rather than here. Followups
set.
That site was no help at all. Here it the part of the program I am
trying to get to work.

/**********************************************
* Get number of days in a month.
***********************************************/

// THIS IS THE PROBLEM AREA!!!
public void numOfDaysInMonth()

Well, it's part of the problem. The `void' piece means
that numOfDaysInMonth() is not going to return any kind of
value -- and then when you *do* attempt to return one, the
compiler spots the mismatch and complains. From the comment
it appears you do in fact want to return a value, so you
should specify what kind of value you intend to return:
`public int numOfDaysInMonth()', most likely.
//
{
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12))
return 31;

if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
year = isLeapYear;

if (month == 2)
if(isLeapYear == year)

return 29;
else
return 28;

Here's the other part of the problem, or rather "after
here." You think you've covered all the bases by checking
all the possible `month' values from 1 to 12, so that one
of the above `return' statements will surely be executed.
But the compiler doesn't know that `month' is between 1 and
12; as far as it knows, `month` could conceivably be 42 or
-1073741824. And if `month' isn't in 1..12 none of the
above `return' statements will be executed and you'll keep
on executing past this point.

(In fact, you seem to think it's possible, too, or you
would presumably not have written the code that appears
below. It can't be executed if `month' is in 1..12, so
why write it in the first place? -- unless, of course, you
and the compiler both think `month' could be 42.)

The point is that the compiler thinks it's possible for
execution to bypass all the `return' statements above and
get to this point in the method. If it does, what will
happen? Since there are no `return' statements after this
point (and no loop that would take you back up to the start
again), if the method gets this far it will not execute a
`return'. So when you fix the method definition, above,
and promise that you're going to return a value, the compiler
tries to ensure that you will *always* return a value, that
there's no way you can simply "fall off the end" of the method
without doing so.

Now, fixing the problem isn't just a matter of removing
all the code below this point. You've got to write the code
in such a way that the compiler can prove to itself that some
kind of value will always be returned, no matter what path
the execution takes through the method. One way would be to
remove the `if (month == 2)' piece above: You've eliminated
the other eleven months, so it must be February, right? And
the compiler can then see that no matter how the isLeapYear
test goes, one or the other of the `return' statements will
be executed.

But that sort of "fix" leaves me with a queasy feeling,
because if `month' somehow does manage to get a value like
-42 the method will happily report 28 or 29 days. That can
be justified by the GIGO principle, but since Java offers a
convenient and superior way to deal with such things it seems
negligent not to use it. The word I'm closing in on is
"Exception," and this would be a great place to throw one:
something's gone rather badly wrong if `month' is 99, so it
makes sense to bring the unusual circumstance to somebody's
attention.

But perhaps you don't know about Exceptions yet. In that
case, I'd suggest a `return 0;' as a sort of anemic substitute:
Somebody gives you an outrageous `month', so you return an
"answer" that's easily distinguished from reality.

... and whether you `return 0;' or use `throw', the
compiler will be satisfied because it now knows that you can
only get out of the method in sanctioned ways.

And what *is* all this extra code for, anyhow? The `if'
statement below is particularly unenlightening ...
for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
monthly[row][column] = column + row;
}

for (int i = 1800; i < year; i++)
{
if (isLeapYear == isLeapYear)
total = total + 366;
else
total = total + 365;
}

for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
System.out.print(" " + monthly[row][column]+ " ");
System.out.println();
}
}
}
 
C

Cid

I am creating a program, but keep receiving the same error what I am
trying to do is have it output a calendar. I have it where it outputs
the month name and year inputted, and the days of the week. I can not
however get it to return a value. It says "cannot return a value from
method whose result type is void". If I change it to anything else I
get the error "missing return statement". I have already been to:
http://java.sun.com/docs/books/tutorial/java/javaOO/methoddecl.html#returnvalue

Sometimes it helps to read things more than once. Also, you may need
to start from an earlier place in the tutorial. Failing these, you'll
want a good intro book on java.

That said, it looks like you want to return an integer value from your
function. So you should declare it as:

public int numOfDaysInMonth()

instead of

public void numOfDaysInMonth()

Lastly, comp.lang.java.help is a better group for beginner questions
like this.
 
D

Dave

The problem is that not all paths through the code return a value.

You need to change the return type to int and you need to be sure that
all paths through the code return an int.

For example, if you add:

return 0;

as the last line of the method your compile error should disappear.
Only you can determine what the proper return value should be, but that
at least gets past the compile error.
 
F

FISH

I am creating a program, but keep receiving the same error what I am
trying to do is have it output a calendar. I have it where it outputs
the month name and year inputted, and the days of the week. I can not
however get it to return a value. It says "cannot return a value from
method whose result type is void". If I change it to anything else I
get the error "missing return statement". I have already been to:


Sometimes one error will mask anothers. I remember a couple of years
ago one Java novice posted his code to a web forum complaining that
he was getting one single compilation error on the first line. When
it was pointed out to him that the line contained a syntax error, he
retorted that he knew it did, but when he'd corrected the syntax the
compiler had given him over a hundred errors in the following code!
(True story!!)

Obviously if the method returns a value, it must say so in its defin-
ition. The true error is the "missing return statement". Take a close
look at the code in your method, and you'll see there is a route through
the code which allows the end of the method to be reached, and no return
value specified. Java is very strict on these things - a method either
always or never returns something - there isn't a 'sometimes' option. So
you need to ammend the code so all routes out will end in a valid return
statement.

-FISH- ><>
 
F

FISH

[email protected] (Charles) wrote in message news: said:
/**********************************************
* Get number of days in a month.
***********************************************/

// THIS IS THE PROBLEM AREA!!!
public void numOfDaysInMonth()
//
{
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12))
return 31;

if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;

if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
year = isLeapYear;

if (month == 2)
if(isLeapYear == year)

return 29;
else
return 28;

for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
monthly[row][column] = column + row;
}

for (int i = 1800; i < year; i++)
{
if (isLeapYear == isLeapYear)
total = total + 366;
else
total = total + 365;
}

for (row = 0; row < 5; row++)
{
for (column = 0; column < 7; column++)
System.out.print(" " + monthly[row][column]+ " ");
System.out.println();
}
}
}




As a quick PS... when dealing with data like the above, it is often
easier and more efficient to use a lookup array rather than a long
involved condition statement.

private static final int[] DAYS_IN_MONTH = { 31,28........ };
:
:
:
// Leap year code here, then...
if(leapYear && month==2) return 29;
else return DAYS_IN_MONTH[month-1];


-FISH- ><>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top