File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem with return statement.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with return statement...." Watch "Problem with return statement...." New topic
Author

Problem with return statement....

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


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


You down with OOP? Yeah you know me!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Hint: When the array is empty, are you returning anything ?


[My Blog]
All roads lead to JavaRanch
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

Joined: Jul 28, 2006
Posts: 37
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Aug 02, 2006
Posts: 109


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

Joined: Dec 06, 2005
Posts: 1321



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.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 03, 2006
Posts: 490
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.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with return statement....
 
Similar Threads
stack generic
Stacks Need Help
stack
read an expression and solve it...
Stacks