Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Problem with return statement....

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic