I'm trying to evaluate a postfix expression.
My code compiles but the final answer is wrong.
I tried looking at other answers but they weren't in Java.
public class PA36Stack
{
public static void main(String[] args)
{
PA31Stack an = new PA31Stack(12);
String g = "234*+";
int x = evaluate(an, g);
System.out.print(x);
}
public static int evaluate(PA31Stack b, String g)
{
int temp = 0;
for (int i = 0; i < g.length(); i++)
{
if (g.charAt(i) != '+' && g.charAt(i) != '-' && g.charAt(i) != '*' && g.charAt(i) != '/')
{
b.push(g.charAt(i));
}
else
{
int a = b.pop();
int c = b.pop();
if (g.charAt(i) == '+')
{
temp = a + c;
b.push(temp);
}
//nextone
if (g.charAt(i) == '-')
{
temp = (c - a);
b.push(temp);
}
//two
if (g.charAt(i) == '*')
{
temp = (c * a);
b.push(temp);
}
//three
if (g.charAt(i) == '/')
{
temp = (c / a);
b.push(temp);
}
}
}
return b.pop();
}
}