# Need help, postfix calculator with stack

Page 1 of 1

## 2 Replies - 6694 Views - Last Post: 03 February 2011 - 11:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=213924&amp;s=e1dfa052920d5b7c9a69acc2191f19f1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ellemrac721

Reputation: 0
• Posts: 10
• Joined: 03-February 11

# Need help, postfix calculator with stack

Posted 03 February 2011 - 06:33 PM

Hi! I really need some help with a previous assignment we had because it's driving me crazy. This was my first version of the postfix calculator, but I had to abandon the idea and make a new one because I couldn't get this one to work in time for our submission deadline I've tried everything I can think of but I still keep getting the wrong answer when I try out the code I wrote below. Things start to go wrong by the "else if(isOperator(s) == true)" part. I don't understand if I wrote anything wrong, but for some reason, it just returns 0 all the time.

```public String calculateFail(String input)
{
StringTokenizer st = new StringTokenizer(input);
Integer temp = 0;
for(int n = 0; n < (st.countTokens() + 1); n++){
String s = st.nextToken();
if(isInteger(s) == true){
calc.push(Integer.parseInt(s));
}
else if(isOperator(s) == true){
temp = calc.pop();
Integer i = calc.pop();
if(s.equals("+")){
temp = i + temp;
} else if(s.equals("-")){
temp = i - temp;
} else if(s.equals("*")){
temp = i * temp;
} else if(s.equals("/")){
temp = i / temp;
}

if(calc.size() == 0){
calc.push(i);
}
}

else{
System.out.println("Please enter a valid postfix expression.");
}
}
return temp.toString();
}

```

And here's the code for the isInteger and isOperator methods that the method above uses:
```    private boolean isInteger(String in){
try{
int x = Integer.parseInt(in);
return true;
}

catch(NumberFormatException error){
return false;
}
}

/**
* Private method that checks if the String is an operator.
* Used in the calculatorFail method.
*
* @return  Returns true if the String is an operator.
*/
private boolean isOperator(String in){
String[] op = {"+", "-", "*", "/"};
for (int i = 0; i < op.length; i++){
if(in == op[i]){
return true;
}
}
return false;
}

```

Is This A Good Question/Topic? 0

## Replies To: Need help, postfix calculator with stack

### #2 Alex_H

Reputation: 22
• Posts: 44
• Joined: 27-January 11

## Re: Need help, postfix calculator with stack

Posted 03 February 2011 - 07:53 PM

The problem you are encountering is with the line
```in == op[i]
```
in the isOperator() method.

Whenever you are comparing two strings, you should always use the equals operator that is in the string class. So line 22 of the second block of code above should be written as:

```  if(in.equals(op[i])){
```

Hope this helps

### #3 ellemrac721

Reputation: 0
• Posts: 10
• Joined: 03-February 11

## Re: Need help, postfix calculator with stack

Posted 03 February 2011 - 11:13 PM

Hi! Thanks for the reply! I tried your suggestion but it still didn't work But of course it should have been like that from the beginning, I changed it now

```private boolean isOperator(String in){
String[] op = {"+", "-", "*", "/"};
for (int i = 0; i < op.length; i++){
if(in.equals(op[i])){;
return true;
}
}
return false;
}

```