954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error In EVALUATE POSTFIX program (Program Getting Compiled)

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();
}
}

red_ferrari007
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Please edit your post, select the code and wrap it in code tags. Use the [CODE] icon above the input box.

You forgot to post the full text of the error message so we could see where it occurred and what the value of the index was.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: