Results 1 to 7 of 7
 02282009, 04:26 AM #1Member
 Join Date
 Feb 2009
 Posts
 5
 Rep Power
 0
[SOLVED] Evaluating an Airthmetic Expression
Hi All,
I need to write a Java program, which reads and Airthmetic Expression and evaluates it.
I have done some part of the program, using classes, but got stuck in coding the Evaluation method. But let me admit that I do have some idea of evaluating the inputted expression.
NOTE: Its assumed that user always enters valid expression and the expression has only *,/,+ and  operators only.
Evaluation method:
I am reading a valid arithmetic expression and looking for the first occurrence of * or / sign and then from that index I am taking the string on the left side of the sign and string to right side of the sign.
Then in Left String I am looking for the lastindexOf( ) function which will give me the last occurrence of the + or  sign(since from the precedence rule Multiplication/Division is first and then + )
And on the Right side I am looking for indexOf( ) string function which will give me the index of the first occurrence of the operator + ,  , / or * . Based on these two left and right indices I am extracting two operands and one operator(either * or / whichever comes first) and performing the operation between them and will update the string with this operation result.
I will be doing this till my expression has only + and  operators and then at the end I can evaluate the string.
But now what I did, is very long and its not even giving the required result.
I will really appreciate if any one can help me with this.
Thanks,
learning_javaNow
 02282009, 05:25 AM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 12
why not make yourself an example and walk through your algorithm step by step to see where you went wrong? or some pseudocode.
 02282009, 05:47 AM #3Member
 Join Date
 Feb 2009
 Posts
 5
 Rep Power
 0
 02282009, 09:28 AM #4Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 12
you don't need to implement your own stack, but it's one way to solve the problem. there's probably a few good ways to do this. personally, i find recursion to be the simplest.
btw, a google search would get you this link, which is probably similar (but possibly more advanced) than what you're trying to do: Sample Code to Evaluate an Infix Expression
 02282009, 04:20 PM #5Member
 Join Date
 Feb 2009
 Posts
 5
 Rep Power
 0
Thank you that was helpful.
 02282009, 04:21 PM #6Member
 Join Date
 Feb 2009
 Posts
 5
 Rep Power
 0
But the thing I am asked to do it without recursions, since its more like How to use different methods in String class.
 03012009, 04:54 AM #7Member
 Join Date
 Feb 2009
 Posts
 5
 Rep Power
 0
Help with Evaluatig an Airthmetic expression code
Can any body help me with this execution error???
I have written this program, but it shows me an error saying that "} is expected", though I have put all brackets properly.
this is my code:
import java.util.Scanner;
class AirthExp
{
private String e;
AirthExp()
{
e = "";
}
AirthExp(String s)
{
e = s;
}
/*public void set(String s)
{
e = s;
}*/
public void print()
{
System.out.println(e.substring(0, e.length()1));
}
public int evaluate()
{
int n1=0, n2=0;
int op1 = 1, op2 = 1;
for(int i=0; i<e.length(); i++)
{
if(e.charAt(i)=='*' e.charAt(i) =='/' e.charAt(i)=='#')
{
String eleft = e.substring(0,i);
String eright = e.substring(i+1, e.length());
op1 = eleft.lastIndexOf('+');
op2 = eright.lastIndexOf('');
if(op1 > op2)
n1 = Integer.parseInt(eleft.substring(op1+1, i));
else
n2 = Integer.parseInt(eleft.substring(op2+1, i));
for(int k = 0; k < eright.length()1; k++)
{
if(eright.charAt(k)== '+'  eright.charAt(k)== '' eright.charAt(k)== '*'  eright.charAt(k)== '/' eright.charAt(k)== '#')
{
String erightNum = eright.substring(0, k);
n2 = Integer.parseInt(erightNum);
}
}
if(e.charAt(i)=='*')
n1 = n1 * n2;
else if(e.charAt(i) =='/')
n1 = n1 / n2;
}
return n1;
}
}
class AirthExpDemo
{
public static void main(String [] args)
{
Scanner keyin = new Scanner(System.in);
String str;
System.out.println("Please Enter a valid Airthmetic Expression followed by #:");
str = keyin.next();
AirthExp expStr = new AirthExp(str);
expStr.print();
System.out.println("= ");
}
}
Similar Threads

Quick reg. expression help
By RR_QQ in forum New To JavaReplies: 4Last Post: 02102009, 07:51 PM 
illegal start of expression
By razmyasdfg in forum CLDC and MIDPReplies: 2Last Post: 07272008, 10:44 PM 
Complex Regular Expression HELP
By hiklior in forum New To JavaReplies: 1Last Post: 04302008, 01:52 PM 
Airthmetic statement shortcuts
By Java Tip in forum Java TipReplies: 0Last Post: 12172007, 10:24 AM 
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07112007, 04:11 AM
Bookmarks