Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with return statement....

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


ok in the code above, im having a problem with the pop() method...



i have a return statement, so why is the compiler telling me im

missing it??

Justin
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: When the array is empty, are you returning anything ?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i cant return -1 or 0, cause is has to be of type T

and the stack class doesnt know what thats going to be yet..


I'm pretty much lost here...

Justin
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask yourself: what should be the result of popping an empty stack? What's the first thing that comes to mind?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an error message, but i added that, and it still gave me

a missing return statement error..

i had a throw new IndexOutOfBoundsException(-1);

but it threw it every time it called it....

Justin
[ August 29, 2006: Message edited by: Justin Fox ]
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You can see that the return statement is a part of the if block. What if the if block doesn't execute (ie if isNotEmpty() returns false). Then there is no return statement executed in the method.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Look you are not returning anything in the pop method block.
while you are using a return statement in a if condition, it is in
scope of if statement not in method block.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dude, i know that...

what am i supposed to return?

if i just throw a

return array[top];

in there...

ill eventually get a boundsexception

and my application will terminate...

and if i remove the if loop, same thing...

thats what i need to know...

what should i put....

or point me in some kind of direction...

dont tell me what i already know...

thanks,

Justin
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should do the other way around. Not checking if it's not empty, but checking if it's empty.

If it's empty, throw something (not your keyboard), like java.util.EmptyStackException.

If it's not empty, pop blop wiiiiz
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
dude, i know that...

what am i supposed to return?

if i just throw a

return array[top];

in there...

ill eventually get a boundsexception

and my application will terminate...

and if i remove the if loop, same thing...

thats what i need to know...

what should i put....

or point me in some kind of direction...

dont tell me what i already know...

thanks,

Justin


Why are you being rude?

You obviously didn't know because you were asking why the compiler was complaining about no return statement.

As was noted, your design needs to be rethought.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:

i had a throw new IndexOutOfBoundsException(-1);

but it threw it every time it called it....


Then you were on the right track, but did something wrong. What did your code look like?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic