我的代码完全可以工作,但我仍然得到错误"无法从对象转换为char",我想知道是否有人可以向我解释问题是什么,以及我是否可以以另一种方式做到一个错误.
这是导致错误的代码char op = (char) term;
.它是从中缀到后缀转换器
//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
}
Run Code Online (Sandbox Code Playgroud)
任何帮助(甚至指向我阅读)都将非常感激.
你可以改变这一行:
char op = (Character) term;
Run Code Online (Sandbox Code Playgroud)
说明:在Java 6中,您无法将其Object
转换为基本类型,但您可以将其Character
强制转换为(这是一个类),其余的则取消装箱:)
编辑: 或者,您可以将项目的语言级别提升到Java 7(或8 ...)
归档时间: |
|
查看次数: |
9662 次 |
最近记录: |