Question

I am writing a code that evaluate Postfix expressions. The numbers are extremely big so I've decided to use BigInteger. I am supposed to store two numbers into linked lists and evaluate their sum, store the result in a third list and then display the answer. However, I get the following exception: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

public static void pfEval(String exp)
{
    Stack s2 = new Stack();
    int i=0;
    BigInteger ans = BigInteger.valueOf(0);
    BigInteger op1 = BigInteger.valueOf(0);
    BigInteger op2 = BigInteger.valueOf(0);
    while(i<exp.length()-1)
    {
        BigInteger op = BigInteger.valueOf(0);
        if(exp.charAt(i) == ' ')
            i++;
        if((exp.charAt(i) >= '0')&&(exp.charAt(i) <= '9'))
        {
            while(exp.charAt(i) != ' ')
            {
                op = op.multiply(BigInteger.valueOf(10));
                op = op.add(BigInteger.valueOf(exp.charAt(i)-48));
                i++;
            }
            s2.push(op);
        }
        else
        {
            op1 = BigInteger.valueOf((long)s2.pop());
            op2 = BigInteger.valueOf((long)s2.pop());
            switch(exp.charAt(i))
            {
            case '+': ans = addition(op2, op1);
            break;
            }
            s2.push(ans);
        }
        i++;
    }
    System.out.println("Answer is "+s2.pop());
}

This is the addition function:

public static BigInteger addition(BigInteger op2, BigInteger op1)
{
    int i, j;
    BigInteger base = BigInteger.valueOf(1000000);
    BigInteger res = BigInteger.valueOf(0);
    LinkedList l1 = new LinkedList();
    LinkedList l2 = new LinkedList();
    LinkedList l3 = new LinkedList();
    while(!op1.equals(0))
    {
        l1.add(op1.mod(base));
        op1 = op1.divide(base);
    }
    while(!op2.equals(0))
    {
        l2.add(op2.mod(base));
        op2 = op2.divide(base);
    }
    if(l1.size()<l2.size())
    {
        for(i=0, j=0; i<l1.size(); i++, j++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));
        }
        while(j<l2.size())
        {
            l3.add(l2.get(i));
        }
    }
    else if(l1.size()>l2.size())
    {
        for(i=0, j=0; i<l2.size(); i++, j++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));
        }
        while(j<l1.size())
        {
            l3.add(l1.get(i));
        }
    }
    else if(l1.size()==l2.size())
    {
        for(i=0; i<l1.size(); i++)
        {
            l3.add(((BigInteger)l1.get(i)).add((BigInteger)l2.get(i)));                 }
    }
    for(i=0; i<l3.size(); i++)
    {
        res = res.add(((BigInteger)l3.get(i)).multiply(base.pow(i)));

    }
    return res;
}

I don't understand what the problem is. Can anyone help me?

Was it helpful?

Solution

Look at this line:

(long)s2.pop()

Then, go back, and look at what you are putting on the stack with s2.push()

And see if you can identify the problem :>

OTHER TIPS

You should be casting as below,

op1 = (BigInteger) s2.pop();
op2 = (BigInteger) s2.pop();

The Stack s2 is already holding object of type BigInteger, So you need to cast it with type BigInteger.

I advice you to use Generic collection, in your case instead of declaring like this,

Stack s2 = new Stack();

You can define as below,

Stack<BigInteger> s2 = new Stack<BigInteger>();

So you can avoid unnecessary casting. And simply use the pop method as below,

op1 = s2.pop();
op2 = s2.pop();

Make Stack generic

Stack<BigInteger> s2= new Stack<>();

and there will be no need in casting

op1 = s2.pop();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top