This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Problem with return statement....  RSS feed

 
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
 
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
 
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 ]
 
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.
 
Ranch Hand
Posts: 1325
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
 
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.
 
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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!