transform the expression

import java.io.*;
import java.util.Stack;

class test 
{


    public static void main(String[] args)throws IOException 
    {

        Stack op;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        op = new Stack();
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            try
            {
                char inp[] = br.readLine().toCharArray();

                for(int i=0;i<inp.length;i++)
                {
                    char c = inp[i];
                    if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
                    {    
                        op.push(c);    
                    }
                    else if(c=='(')
                    {
                        op.push(c);    
                    }
                    else if(c==')')
                    {
                        while(op.lastElement()!='(')       //giving error in this line
                        {
                            System.out.print(op.pop());
                        }
                        if(op.capacity()>1)
                        {
                            op.pop();
                        }
                        else
                        {
                            op.clear();
                        }

                    }

                    else
                    {
                        System.out.print(c);
                    }
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();;
            }
        }
    }
}

this code is running fine in my net beans but giving a compilation error
for the usage

while(op.lastElement()!='(')

or while(op.peek()!='(')

plz help...

You are using a Stack.

It can only store and retrieve Objects.
Use a Character Stack and replace

Stack op;

by

Stack <Character> op;

This should get you AC. :slight_smile:

1 Like

@deepankarak thankyou got AC :wink:

@zargus Do give karma by accepting the answer and so that the question can be closed.