Question: Help?? why am I getting an error? import java.util.Stack; import java.util.Scanner; public class PostfixEvaluator { private final static char ADD = ' + ' ;

Help?? why am I getting an error?
import java.util.Stack;
import java.util.Scanner;
public class PostfixEvaluator
{
private final static char ADD ='+';
private final static char SUBTRACT ='-';
private final static char MULTIPLY ='*';
private final static char DIVIDE ='/';
private final static char REMAINDER ='%';
private final static char MINUS ='m';
private final static char EXCHANGE ='r';
private final static char DUPLICATE ='d';
private final static char PRINT ='p';
private final static char PRINTNREMOVE ='n';
private final static char ALL ='f';
private final static char CLEAR ='c';
private final static char HELP ='h';
private Stack stack;
public PostfixEvaluator()
{
stack = new Stack();
}
public int evaluate(String expr)
{
int op1, op2, result =0;
String token;
Scanner scanner = new Scanner(expr);
while (scanner.hasNext())
{
token = scanner.next();
if (isOperator(token))
{
op2=(stack.pop()).intValue();
op1=(stack.pop()).intValue();
result = evaluateSingleOperator(token.charAt(0), op1, op2);
stack.push((result));
}
else
stack.push((Integer.parseInt(token)));
}
return result;
}
private boolean isOperator(String token)
{
return ( token.equals("+")|| token.equals("-")||
token.equals("*")|| token.equals("/"));
}
private int evaluateSingleOperator(char operation, int op1, int op2)
{
int result =0;
switch (operation)
{
case ADD:
result = op1+ op2;
break;
case SUBTRACT:
result = op1- op2;
break;
case MULTIPLY:
result = op1* op2;
break;
case DIVIDE:
result = op1/ op2;
case REMAINDER:
result = op1/ op2;
case MINUS:
stack.push(-stack.pop());
break;
case EXCHANGE:
op1= stack.pop();
op2= stack.pop();
stack.push(op1);
stack.push(op2);
break;
case DUPLICATE:
int top = stack.peek();
stack.push(top);
break;
case PRINT:
System.out.println(stack.peek());
break;
case PRINTNREMOVE:
System.out.println(stack.pop());
break;
case ALL:
for (int i =0; i < stack.size(); i++)
{
System.out.println(stack.pop());
}
break;
case CLEAR:
stack.clear();
break;
case HELP:
System.out.println("Enter the expression using the following integers:
+,-,*,/,%, m, r, d, p, f, n, c, q, h");
break;
default :
System.out.println("Invalid input.");
}
return result;
}
}
import java.util.Scanner;
/**
* Demonstrates the use of a stack to evaluate post fix expressions.
*
*
*/
public class PostFixTester
{
public static void main(String[] args)
{
String expression, again;
int result;
Scanner in = new Scanner(System.in);
do
{
PostfixEvaluator evaluator = new PostfixEvaluator();
System.out.println("Enter a valid post-fix expression one token "+
"at a time with a space between each token (e.g.54+321-+*)");
System.out.println("Each token must be an integer or an operator (+,-,*,/)");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals "+ result);
System.out.print("Evaluate another expression [Y/N]?");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
in.close();
}
}
public class InStack
{
private int[] stack;
private int top;
public InStack(int capacity)
{
stack = new int[capacity];
top =-1;
}
public void push(int value)
{
stack[++top]= value;
}
public int pop()
{
return stack[top--];
}
public int peek()
{
return stack[top];
}
public boolean isEmpty()
{
return top ==-1;
}
public int size()
{
return top +1;
}
public void clear()
{
top =-1;
}
}
Enter a valid post-fix expression one token at a time with a space between each token (e.g.54+321-+**)
Each token must be an integer or an operator (+,-,**,//)
23 r 3+5
ERROR

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!