2013-11-14 2 views
0

У меня есть эти Программы, которые оценивают выражение Lisp в Java, и я получаю странную ошибку.error incontvertible type "intvalue()"

Во-первых, это программа:

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 */ 

и ошибка во время компиляции:

IterativeEvaluator2.java:69: cannot find symbol 
symbol : method intvalue() 
location: class java.lang.Double 
       char operator = (char) (some_queue.remove()).intvalue(); 
                  ^
IterativeEvaluator2.java:69: inconvertible types 
found : java.lang.Double.intvalue 
required: char 
       char operator = (char) (some_queue.remove()).intvalue(); 
                    ^
Note: IterativeEvaluator2.java uses unchecked or unsafe operations. 
Note: Recompile with -Xlint:unchecked for details. 
2 errors 

Любые предложения о том, почему он не работает? Я попытался выставить выражение

char operator = (char) ((int) some_queue.remove()).intvalue(); 

но это не решило мою проблему. Спасибо

+0

Где вы видите это 'intvalue)' метод (? (Подсказка: случай персонажа) –

ответ

2

У вас есть орфографическая ошибка в имени метода. «V» капитализируется (camelCase) в intValue. Изменение

intvalue(); 

в

intValue(); 
+0

@Downvoter Пожалуйста, укажите, что вы считаете неправильным или можете улучшить. – rgettman

+0

О, это просто смешно. Спасибо, rgettman! – kolonel

+0

@kimos Пожалуйста, не забывайте принимать ответы, которые решают вашу проблему. – chrylis

1
char operator = (char) (some_queue.remove()).intvalue(); 
              ^

три проблемы:

  1. орфография ошибка intvalue(): она должна быть intValue()
  2. ваш кастинг (char) (some_queue.remove()) будет пытаться бросить Double - char Первый: char - примитивный тип. Вы не можете отнести его к экземпляру типа класса Double. кроме того, не может быть intValue() функция.

  3. some_queue является очередью типа: Queue<Double>: отсюда правильный путь, чтобы сделать:

    char operator = (char)(some_queue.remove().intvalue()); 
                    ^