PROGRAM GETS COMPILED...BUT GIVES ERROR ARRAY OUT OF BOUNDS...PLEASE HELP AS SOON AS POSSIBLE !!!
import java.io.*;
class Evaluate
{
char postfix[];
int stack[];
int i,a,b,c,top,n;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
Evaluate() throws IOException
{
System.out.println("ENTER ARRAY SIZE");
n=Integer.parseInt(input.readLine());
postfix=new char[n];
stack=new int[n];
top=-1;
i=-1;
}
void readpostfix() throws IOException
{
char p;
System.out.println("ENTER STRING");
do
{
p=(char)input.read();
i++;
postfix[i]=p;
}
while(p!='\n');
postfix[i]='\0';
}
boolean isoperand(char c)
{
if(c>='0'&&(c<='9'))
return true;
else
return false;
}
void compute()
{
for(i=0;postfix[i]!='\0';i++)
if(isoperand(postfix[i]))
{
top++;
stack [top]= (int) (postfix[i]-'0');
}
else
{
a=stack[top];
top--;
b=stack[top];
switch(postfix[i])
{
case'+':c=b+a;
stack[top]=c;
break;
case'-':c=b-a;
stack[top]=c;
break;
case'*':c=b*a;
stack[top]=c;
break;
case'/':c=b/a;
stack[top]=c;
break;
default:System.out.println("INVALID OPERATOR");
}
}
System.out.println("THE VALUE OF POSTFIX EXPRESSION IS "+stack[top]);
}
public static void main(String args[])throws IOException
{
Evaluate e=new Evaluate();
e.readpostfix();
e.compute();
}
}