0

Hello,

I am having some difficulty in trying to figure out where I am going wrong in my program. I am trying to convert infix to postfix and from there I am evaluating the postfix. However, it is not compiling correctly. I've tried several things but nothing seems to work correctly for me.

```
import java.util.*;
import java.io.*;
public class calculator {
private String infix;
private String postfix = " ";
// public calculator(String in) {
// input = in;
// int SIZE = input.length();
// }
public String toPostfix(String infix) {
String expression;
String postfix = " ";
Stack operatorStack = new Stack();
StringTokenizer s = new StringTokenizer(infix);
while (s.hasMoreTokens()) {
expression = s.nextToken();
if (Character.isDigit(expression.charAt(0)))
postfix = postfix + " " + (Integer.parseInt(expression));
else if (expression.equals("("))
{
Character operator = new Character('(');
operatorStack.push(operator);
} else if (expression.equals(")"))
{
while (((Character) operatorStack.peek()).charValue() != '(') {
postfix = postfix + " " + operatorStack.pop();
}
operatorStack.pop();
} else
{
while (!operatorStack.isEmpty()
&& !(operatorStack.peek()).equals("(")
&& prec(expression.charAt(0)) <= prec(((Character) operatorStack
.peek()).charValue()))
postfix = postfix + " " + operatorStack.pop();
Character operator = new Character(expression.charAt(0));
operatorStack.push(operator);
}
}
while (!operatorStack.isEmpty())
postfix = postfix + " " + operatorStack.pop();
return postfix;
}
private static int prec(char x) {
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
public int evaluate(String postfix) {
int a;
int b;
int result = 0;
Stack<Integer> myStack = new Stack<Integer>();
for (int i = 0; i < postfix.length(); i++) {
char ch = postfix.charAt(i);
if (ch >= '0' && ch <= '9') {
myStack.push((int) (ch - '0'));
} else {
switch (ch) {
case '+':
a = myStack.pop();
b = myStack.pop();
result = a + b;
myStack.push(result);
break;
case '-':
a = myStack.pop();
b = myStack.pop();
result = a - b;
myStack.push(result);
break;
case '*':
a = myStack.pop();
b = myStack.pop();
result = a * b;
myStack.push(result);
break;
case '/':
a = myStack.pop();
b = myStack.pop();
result = a / b;
myStack.push(result);
break;
}
myStack.push(result);
}
}
myStack.push(result);
return result;
}
}
```