Java 不可转换类型错误“;intvalue();
我有一个用Java计算Lisp表达式的程序,我得到了一个奇怪的错误 首先,这是一个计划:Java 不可转换类型错误“;intvalue();,java,Java,我有一个用Java计算Lisp表达式的程序,我得到了一个奇怪的错误 首先,这是一个计划: import java.util.Queue; import java.util.LinkedList; import java.util.Stack; import java.util.ArrayList; class IterativeEvaluator { private ExpressionScanner expression; public IterativeEvalua
import java.util.Queue;
import java.util.LinkedList;
import java.util.Stack;
import java.util.ArrayList;
class IterativeEvaluator
{
private ExpressionScanner expression;
public IterativeEvaluator (String expression)
{
this.expression = new ExpressionScanner(expression);
}
public double evaluate(Queue<Double> operandqueue)
{
// write your code here to create an explicit context stack
Stack<Queue> temp_stack = new Stack<Queue>();
char operator = ' ';
double operand = 0.0;
// write your code here to evaluate the LISP expression iteratively
// you will need to use an explicit stack to push and pop context objects
while ( expression.hasNextOperator() || expression.hasNextOperand() )
{
// Get the open bracket
if ( expression.hasNextOperator())
{
operator = expression.nextOperator() ;
if (operator == '(')
{
// if the stack is empty then the first bracket is trivial
// so this method will be instantiated by null
if (temp_stack.empty())
temp_stack.push(operandqueue);
// else instantiate an arraylist(Queue)
else {
operandqueue = new LinkedList<Double>();
}
}
// push the list into the stack after the closing bracket appears
else if (operator == ')')
{
operand = calculate(operandqueue);
operandqueue = temp_stack.pop();
operandqueue.offer(operand);
}
// if it is another operator then it must be +,-,/,*
else {
operator = expression.nextOperator();
operandqueue.offer( (double) operator );
}
}
// else it is an operand so just put it in the queue
else
operandqueue.offer( (double) expression.nextOperand() );
}
return operand;
}
private double calculate(Queue<Double> some_queue)
{
char operator = (char) (some_queue.remove()).intvalue();
double operand1 = 0;
double operand2;
switch(operator){
case '+' : while( !some_queue.isEmpty() )
{
operand2 = some_queue.remove();
operand1 = operand1 + operand2;
}
break;
case '-' : operand1 = some_queue.remove();
//checks for negative numbers
if (some_queue.isEmpty() )
operand1 = 0 - operand1;
else{
while ( !some_queue.isEmpty() )
{
operand2 = some_queue.remove();
operand1 = operand1 - operand2;
}
}
break;
case '*' : operand1 = 1;
while ( !some_queue.isEmpty() )
{
operand2 = some_queue.remove();
operand1 = operand1*operand2;
}
break;
case '/' : operand1 = some_queue.remove();
if (some_queue.isEmpty() )
operand1 = 1/operand1 ;
else{
while (!some_queue.isEmpty())
{
operand2 = some_queue.remove();
operand1 = operand1/operand2; }
}
break;
}
return operand1;
}
public static void main(String [] args)
{
String s =
"(+\t(- 6)\n\t(/\t(+ 3)\n\t\t(- \t(+ 1 1)\n\t\t\t3\n\t\t\t1)\n\t\t(*))\n\t(* 2 3 4))"; // = 16.5
IterativeEvaluator myEvaluator = new IterativeEvaluator(s);
System.out.println("Evaluating LISP Expression:\n" + s);
System.out.println("Value is: " + myEvaluator.evaluate(null));
}
} /* 201340 */
关于它为什么不起作用有什么建议吗?我试着表达一下
char operator = (char) ( (int) some_queue.remove()).intvalue();
但这并没有解决我的问题。
谢谢方法名称中有拼写错误。“V”在中大写(camelCase)。改变 到
方法名称中存在拼写错误。“V”在中大写(camelCase)。改变 到
方法名称中存在拼写错误。“V”在中大写(camelCase)。改变 到
方法名称中存在拼写错误。“V”在中大写(camelCase)。改变 到 三个问题:
intvalue()
的拼写错误:应该是intvalue()
(char)(some_queue.remove())
将首先尝试将双字符强制转换为char
:char
是一种基本类型。不能将其强制转换为类类型为Double
的实例。
此外,它不能有intValue()
函数
some_queue
是以下类型的队列:queue
:因此正确的方法是:
char operator = (char)(some_queue.remove().intvalue());
^
intvalue()
的拼写错误:应该是intvalue()
(char)(some_queue.remove())
将首先尝试将双字符强制转换为char
:char
是一种基本类型。不能将其强制转换为类类型为Double
的实例。
此外,它不能有intValue()
函数
some_queue
是以下类型的队列:queue
:因此正确的方法是:
char operator = (char)(some_queue.remove().intvalue());
^
intvalue()
的拼写错误:应该是intvalue()
(char)(some_queue.remove())
将首先尝试将双字符强制转换为char
:char
是一种基本类型。不能将其强制转换为类类型为Double
的实例。
此外,它不能有intValue()
函数
some_queue
是以下类型的队列:queue
:因此正确的方法是:
char operator = (char)(some_queue.remove().intvalue());
^
intvalue()
的拼写错误:应该是intvalue()
(char)(some_queue.remove())
将首先尝试将双字符强制转换为char
:char
是一种基本类型。不能将其强制转换为类类型为Double
的实例。
此外,它不能有intValue()
函数
some_queue
是以下类型的队列:queue
:因此正确的方法是:
char operator = (char)(some_queue.remove().intvalue());
^
您在哪里看到这个
intvalue()
方法?(提示:字符大小写)您在哪里看到这个intvalue()
方法?(提示:字符大小写)您在哪里看到这个intvalue()
方法?(提示:字符大小写)您在哪里看到这个intvalue()
方法?(提示:字符大小写)@Downvoter请指出您认为错误或可以改进的地方。@kimos请记住接受解决您问题的答案。@Downvoter请指出您认为错误或可以改进的地方。@kimos请记住接受解决您问题的答案。@Downvoter请指出您认为错误或可以改进的地方。@kimos请记住接受解决您问题的答案。@请指出您认为错误或可以改进的地方。@kimos请记住接受解决您问题的答案。
char operator = (char)(some_queue.remove().intvalue());
^