0

This section of my code does not appear to be working. I feel like the problem might like with the stk.peek() sections of the code but I am not sure if this is true. Any ideas?

stk is my stack and the first thing read into the code is "("

if(stk.empty()==true || stk.peek()== "("){
    stk.push(post.get(i))      
    System.out.println("Test1:" +stk.peek());
}
user2920249
  • 137
  • 4
  • 13
  • 4
    The issue is that you're not comparing strings properly. Use `.equals()`, not `==`. – awksp May 15 '14 at 22:56
  • 4
    Why write `stk.empty()==true`? Surely `stk.empty()==(1 == 1)` would be more clear. Or, `stk.empty()== ((1 == 1) == (true != false))`? – Boris the Spider May 15 '14 at 22:59
  • @user3580294 What do you mean? I tried multiple variations of that stk.equals('(') stk.equals("(") none seemed to work – user2920249 May 15 '14 at 23:06
  • @BoristheSpider Doesn't stk.empty() use a boolean? I thought that would be easier to write and look at. – user2920249 May 15 '14 at 23:06
  • @user2920249 That's because in those cases you're comparing *the stack* to `"("`, not `stk.peek()`. A stack is obviously not equal to a `String`. You want `stk.peek().equals("(")` – awksp May 15 '14 at 23:08

1 Answers1

3

Try this. This should do it and has somewhat better style.

if (stk.empty() || "(".equals(stk.peek())){
    stk.push(post.get(i));     
    System.out.println("Test1:" + stk.peek());
}
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • This gets me a lot closer. It fixed this section of codes problems. I'm gonna try it on the others. Thank you so much. – user2920249 May 15 '14 at 23:10