1

I am a beginner in java and am writing a stack class using array.

So I have a method in this class called pop

 public int Pop(){
 if (current_size >0)
 { // do something 
 return ele;
 }
 // return nothing <-- ths is where error is
 }

Since i have the return type int.. the class is always expecting to return something. How should I tackle such cases where the method wil return something if the condition is true else it wont shouldnt return anything? Thanks

asked Sep 24, 2012 at 2:37
1
  • There's....a lot wrong here. First, where is your array? You're not using it in this method. Second, it's difficult to decide what to return when the stack is empty, since any primitive int may be a valid entry in the stack. Third, I don't see anything that reduces the size of the stack or adjusts where the end of the stack is pointing to. Commented Sep 24, 2012 at 2:40

5 Answers 5

7

You must always return something (unless your method is void) or throw an exception. You could try this:

public int pop() {
 if (current_size > 0) 
 return ele;
 throw new EmptyStackException();
}
answered Sep 24, 2012 at 2:40
Sign up to request clarification or add additional context in comments.

Comments

3

Without knowing what your desired behavior is, it is hard to tell you, but I can provide a couple suggestions.

  1. If you want to truly return nothing, you should change the return type to be the Integer wrapper class rather than the int primitive. This way you can return null which would be nothing.
  2. If this is an exceptional case, perhaps you want to throw an Exception.

    if (evaluateSomething()) {
     // do something in response...
    } else {
     throw new RuntimeException("You can't do that!");
    }
    
Makoto
107k29 gold badges200 silver badges237 bronze badges
answered Sep 24, 2012 at 2:42

Comments

2

You could raise an exception when the stack is empty, or you could return a special int like -1 if your stack never has negative numbers.

throw new StackIsEmptyException();
Marko
20.6k13 gold badges51 silver badges64 bronze badges
answered Sep 24, 2012 at 2:43

Comments

1

You need to have a return statement or throw an exception for all possible exit point.

answered Sep 24, 2012 at 3:19

Comments

1

Unless your method is void, you must either return a variable or throw/catch an exception. Something like this should do;

 public int pop() 
{
 if (current_size > 0) 
 return ele; //return variable
 throw new EmptyStackException(); //throw new exception
}
answered Mar 16, 2017 at 11:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.