CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: eval in java??

  1. #1
    Join Date
    Aug 2005
    Posts
    134

    eval in java??

    I'm trying to make this basic calculator in java just to expand my knowledge a bit. But i've already came across a problem.
    The application has a textfield where you can enter a sum like: 4+(2*8)

    What i wanted to use is a funtion called eval(), but Java doesn't seem to have this. Is there an equivallent? Or how could i else calculate stuff like that?

    Thanks for any help.

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: eval in java??

    I dont know if there is eval() equalent in Java. However that kind of calculation is algorithvise quite doable. In fact it is not uncommon a practise, and quite surely the net is full of Java calculator how to´s and tutorials.

    You could, for example, break the whole thing to charArray, or use String.indexOf to find the first occurence of... whats it called... these ->( ), parse its contents to a calculation, calculate that and proceed untill all those have been dealt with.

    So in that idea... you need to construct a parser, to which you offer a String, it will determine the numbers and different operators, plus minus and so forth. If the string is calculatable, parser returns the result.

    Surely you have this and that readily made in the bubbling foam of Java community. Short googling gives you eg. beanShell, that appears to do what you need.

    Code:
    Object result = i.eval("2*2");  // Integer
    where i is instance of beanShells interpreter. Not hundred percent sure it can evaluate (3*(5+6)) but would be a little suprised if it could not.

    Check that out, or any of the other available parsers, in case coding your own is not topmost priority right now.
    Last edited by Londbrok; March 18th, 2008 at 03:54 PM.

  3. #3
    Join Date
    Dec 2006
    Posts
    166

    Re: eval in java??

    something like eval() is really difficult in a compiled language, because it would need access to the compiler at runtime

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: eval in java??

    something like eval() is really difficult in a compiled language, because it would need access to the compiler at runtime
    Not true at all. You can write an algorithm that parses the string into a tree where the leaves are numbers and the parents are operators. If you parse the information correctly into the tree then you can simply evaluate the tree.

    I did this in ANSI C a while back for a programming assignment that took the derivate of a function. I'ld post the code but it's a bit messy, and I believe that it was for prefix operations not infix.

  5. #5
    Join Date
    Dec 2006
    Posts
    166

    Re: eval in java??

    eval() usually means a function that can take arbitrary source code as a string and evaluate it

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: eval in java??

    eval() usually means a function that can take arbitrary source code as a string and evaluate it
    I understand, I have taken two functional programming courses. However, for the purposes of a calculator eval can be done relatively simply. You can recursively parse through the string inserting the nodes into a tree and then perform the evaluation. There needs not be any "on the fly" compiling or reworking of any code.

    *
    + 5
    2 3

    would be the tree for (2 + 3) * 5. With no parens it would look like this:

    +
    2 *
    3 5

    for 2 + 3 * 5

    Figuring out how to recurse through the string and correctly insert the items into a tree and then execute when the idea is a simple calculator (with + - * /) does not need to be as complicated as the link you provided.

    As a matter of fact, you can perform infix, prefix and postfix operations on the tree depending on whether you decided to go left-read-right, right-read-left, or read-left-right.

  7. #7
    Join Date
    Aug 2005
    Posts
    134

    Re: eval in java??

    Im trying to write my own eval function.
    Tearing a string apart isn't the difficult part but the calculation is.
    Suppose i have the following string:

    String str = 2*(5+5);

    I mean i can take it apart but how do i eventually calculate it since there is no function at all for that...?? I can't parse it back into an int and put that in a println().

  8. #8
    Join Date
    Apr 2007
    Posts
    425

    Re: eval in java??

    ProgramThis gave you an excellent example of a function you could write to parse the string, and return you your result (as an int, float or double, whatever you decide to implement) which you can put in your println

  9. #9
    Join Date
    Feb 2008
    Posts
    966

    Re: eval in java??

    Once you have the string parsed (whether in a tree, or however you want to do it) you can try to evaluate it, or you can write a dirty, easy hack:
    Code:
    int evaluateOperator(int a, char op, int b) {
       int val = 0;
       switch(op)
           case '+':
               val = a + b;
               break;
           case '-':
               ...//you get the point
          return val;
    }
    I know that isn't a true eval, but for a simple calculator it serves the purpose. It will work as a sort of eval emulator. Parse the string into a series of actions to be performed, iterate through your data structure containing the elements, call this method when you have an operator (using recursion if you have nested operators).

    The point is: you are not going to get Java to work like Lisp, Scheme, Prolog, ML. It's just not going to happen, but you CAN emulate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured