My code works perfectly as far as I can see but I still get the error "Cannot cast from Object to char" and I was wondering if anyone could explain to me what the problem is and if I can do it another way that does not cause an error.
This is the bit of code causing the error char op = (char) term;
. It is from an infix to postfix converter
//Take terms off the queue in proper order and evaluate
private double evaluatePostfix(DSAQueue<Object> postfixQueue) {
DSAStack<Double> operands = new DSAStack<Double>();
Object term;
//While still terms on queue, take term off.
while(!postfixQueue.isEmpty()) {
term = postfixQueue.dequeue();
if(term instanceof Double) { //If term is double put on stack
operands.push((Double) term);
}
else if(term instanceof Character) { //If term is character, solve
double op1 = operands.pop();
double op2 = operands.pop();
char op = (char) term;
operands.push(executeOperation(op, op2, op1));
}
}
return operands.pop(); //Return evaluated postfix
}
Any help (even pointing me to some reading) would be much appreciated.