0
public static boolean xIn(Stack<Integer> st, int x) throws Exception {

    int result;
    int size = st.getSize();

    for (int i = 0; i < size; i++) {
        result = st.pop();
        if (x == result) {
            return true;
        }
        st.push(result);
    }
    return false;
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • ???? My code is logically incorrect. It seems fine to me. You need to have ArrayStack and Stack Interface plus methods for this) – Sayat Otarbay Feb 17 '16 at 19:16
  • 4
    Don't compare objects with ==. Also, popping then pushing in a loop will always compare the same element, that is at top of the stack. Why don't you use Stack.contains()? – JB Nizet Feb 17 '16 at 19:17
  • You shouldn't use == to compare objects, use .equals or better yet, use stack.contains. – matt Feb 17 '16 at 19:18
  • 1
    If you're implementing your own Stack, than this method should be part of the Stack class itself. That way, you just need to iterate over the elements of the stack instead of popping and pushing. – JB Nizet Feb 17 '16 at 19:29
  • 1
    Stack class offers an iterator, So simpliest solution would be `boolean xIn(Stack st, int x){for(Integer i : st)if(i.equals(x))return true;return false;}` – ArcticLord Feb 17 '16 at 19:32
  • @ArcticLord if the OP uses an actual java.util.Stack, then it also has a contains() method. No need to iterate. – JB Nizet Feb 17 '16 at 19:38

4 Answers4

4

When you pop an item from a stack, you remove it from the top. And when you push an item to a stack, you put it on the top.

You are continually checking only the first element on the top of the stack.

Also, another thing, shouldn't you push the element back to the stack just before you return true?

radoh
  • 4,554
  • 5
  • 30
  • 45
0

You always check the last element, popping it and then pushing it again. Use e.g. an enhanced for loop to iterate the stack.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
0

Try this instead:

public static boolean xIn(Stack<Integer> st, int x){

    int result;
    int size = st.getSize();

    for (int i = 0; i < size; i++) {
        if (x == st.get(i)) {
            return true;
        }
    }
    return false;
}

Also, I advice you to have a look here https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
There is alot of predefined methods that do your job such as:
search(obj o)
contains(obj o)

Wazani
  • 911
  • 1
  • 13
  • 28
0
 public static boolean xIn(Stack<Integer> st, int x) throws Exception {
   Stack<Integer> temp=new ArrayStack();
    int result;
    int size = st.getSize();

    for (int i = 0; i < size; i++) {
        result = st.pop();
        if (x == result) {
            return true;
        }
        temp.push(result);
    }
     for (int i = 0; i < size; i++) {
         result=temp.pop();
         st.push(result);
     }

    return false;
}
  • This is wrong, `temp` stack won't help you, since you `return` from the function before you push the elements back to the original stack. Instead of a `return`, use a boolean variable which you return at the end. – radoh Feb 17 '16 at 19:24
  • Pretend you find x, you will return after having removed nearly all of st. You should have probably edited your original question instead of posting a new answer. Also [this question](http://stackoverflow.com/questions/4428774/why-java-does-not-see-that-integers-are-equal) addresses a common pitfal with integers. – matt Feb 17 '16 at 19:25