0

The following java code is to convert from infix to postfix and evaluate postfix expression. I'm getting array index out of bounds exception in class Eval, and don't know why. Please help!!!!
This isn't a duplicate to what has been pointed. I haven't done the mistake indicated in the answer to that question.

import java.util.Scanner;

 class I2F2 
{
    int top = -1;
    char mystack[] = new char[25];
    String  arr = "";



    void push(char ch)
    {
        mystack[++top] = ch;
    }

    char pop()
    {
        return mystack[top--];
    }

    char peek()
    {
        return mystack[top];
    }

    boolean isEmpty(){
    return top==-1;
    }

    int prec(char ch)
    {
        switch(ch)
        {
            case '+': return 1;
            case '-': return 1;

            case '*': return 2;
            case '/': return 2;

            case '^': return 3;
        }
        return 0;
    }

    boolean isAlpha(char ch)
    {
        return (ch >= 'a' && ch<= 'z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9');
    }

    boolean isOperator(char ch)
    {
        return ch=='/' || ch=='*' || ch=='+' || ch=='-' || ch=='^';
    }

    void infixToPost(String str)
    {

        char ch;
        int a = 0;

        for (int i=0; i < str.length(); i++)
        {
            ch = str.charAt(i);
            if (isAlpha(ch))
            {
                arr+= ch;
            }

            else if (ch == '(' || ch == '{' || ch == '[')
            {
                push(ch);
            }        
        else if (ch==')' || ch=='}' || ch==']')
            {               
                while( !(peek() == '(' || peek() == '{' || peek() == '[') )
                {                        
                    arr+= pop();
                }
                pop();
            }

            else if (isOperator(ch))
            {
                if ( isEmpty() || (prec(ch)>prec(peek())) || (peek() == '('))
                    push(ch);            

                  else if (prec(ch)<=prec(mystack[top]))
                {
                    while (prec(ch)<=prec(mystack[top]) )
                    {
                        arr+= pop();
                        if (top == -1)
                            break;
                    }
                    push(ch);                        
                }
            }
        }

        while (top != -1)
            arr+= pop();



            System.out.print(arr);

    }
}
    class Eval
    {
        I2F2 o = new I2F2(); 
        int no1, no2;
        int stack1[] = new int[25];
        int t = -1;

        void push(int i)
        {
         t = t + 1;
         stack1[t] = i;
        }

        int pop()
        {
            int e = stack1[t];
            t = t-1;
            return e;
        }

        int pEval()
        {
        for (int i = 0; i < o.arr.length(); i++)
        {
        char ch = o.arr.charAt(i);
        if (Character.isDigit(ch))
        {
            push(ch);
        }

        else
        {
             no2 = pop();
             no1 = pop();

             switch(ch)
             {
                 case '+': push ((no1+no2)); break;
                 case '-': push(  (no1-no2)); break;
                 case '*': push(  (no1*no2)); break;
                 case '/': push(  (no1/no2)); break;
                 case '%': push(  (no1%no2)); break;
                 case '^': push((int)  (Math.pow(no1,no2))); break;

             }

         }
        }
        return pop();
    }
    }

class MyMain
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter  an infix expression: ");
        String s = sc.nextLine();


        I2F2 obj = new I2F2();
        Eval obj2 = new Eval();

        obj.infixToPost(s);
        System.out.println(obj2.pEval());
    }    
}
Sparsh
  • 25
  • 1
  • 4
  • Man, how is this question a duplicate? I need help finding the bug. Please help. – Sparsh May 08 '16 at 14:12
  • You are most certainly getting the same exception for the same reason. The exception is only thrown for one reason. Your question should have included a stack trace. – user207421 May 08 '16 at 16:48

0 Answers0