سؤال

I am trying to write a code for postfix evaluation but I get a error as

java.lang.String cannot be cast to java.lang.Integer and the problem is in the linesobj1=(int) calStack.topAndpop();.The problem is my ArrayStack topAndpop() method returns a Object type as

 public Object topAndpop() throws EmptyStackException{
    Object returnPop;
        if (isEmpty())
            throw new EmptyStackException("Stack empty");
        else{ 
            returnPop=top();
            pop();
            }
        return returnPop;

and I should be able to convert this into int type.I can't see anything wrong apart from this line.Can someone point me how to correct this please

import java.lang.Math;
public class Calculate{
    double result=0;
    int obj1,obj2;
    public Object cal(String expression) throws OverFlowException,EmptyStackException{

    String[] array = expression.split("");//remember
    // for (int i=0;i<array.length;i++)
        // System.out.println(array[i]);
    ArrayStack calStack=new ArrayStack(array.length);
    for(int i=0;i<array.length;i++){
        if(!(array[i].equals("+") || array[i].equals("-")||array[i].equals("/") || array[i].equals("*"))){
            calStack.push(array[i]);
        //calStack.print();
        }
        else {

            obj1=(int) calStack.topAndpop();//check how this casting is done
            obj2=(int)calStack.topAndpop();
        result=calculate(obj2,obj1,array[i]);
        System.out.println(result);
        calStack.push(result);
        }
    }
    return calStack.topAndpop();

}
public double calculate(int a,int b,String op){
    if(op=="+")
        return a+b;
    else if(op=="-")
        return a-b;
    else if(op=="/")
        return a/b;
    else if (op=="^")
        return Math.pow(a,b);
    else 

        return a*b;

}

public static void main (String args[]) throws OverFlowException,EmptyStackException{
    Calculate c=new Calculate();
    System.out.println("result"+c.cal("623+-382/+*2^3"));

}

}

هل كانت مفيدة؟

المحلول

Instead of

obj1=(int) calStack.topAndpop();//check how this casting is done
obj2=(int)calStack.topAndpop();

Use:

obj1 = Integer.parseInt((String)calStack.topAndpop());
obj2 = Integer.parseInt((String)calStack.topAndpop());

نصائح أخرى

You have more then one issue, first String equality -

public double calculate(int a,int b,String op){
  if(op.equals("+")) // <-- .equals! Not ==
    return a+b;
  else if(op.equals("-"))
    return a-b;
  else if(op.equals("/"))
    return a/b;
  else if(op.equals("^"))
    return Math.pow(a,b);
  else 
    return a*b;
}

Next, since your Stack doesn't appear to be generic, you should call Integer.parseInt(String),

obj1 = Integer.parseInt(calStack.topAndpop().toString());

The problem is in the if condition where you check for symbols, In this you have missed the ^ symbol :

         if(!(array[i].equals("+") || array[i].equals("-")
              ||array[i].equals("/") || array[i].equals("*"))){

Add the condition for ^ symbol as follows and your code will work :

          if(!(array[i].equals("+") 
               || array[i].equals("-")
               ||array[i].equals("/") 
               || array[i].equals("*"))
               || array[i].equals("^"))){

            // do something 
          }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top