By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Version of Python eval() function in Java


New Coder
Hello everyone, I bring you a class that versions the Python eval() function in the Java language. It’s the third attempt and I think it’s a good idea to upload it to github so they can review it. I have tested it a lot but I am sure that the errors will be found by you, since I have found it correct so far. There is some technical information at the beginning of the class. I hope the errors don’t appear too fast :laugh:. All the best!

Impressive work Mario ! That must have taken you a while to write, test, and debug.
Looking at the Java code this seems more like a rudimentary floating point calculator than an emulation of the Python eval function. I'm not a Python expert but from the documentation I gather that the Python eval function can pretty much evaluate any Python statement, whereas your Java version, if I'm not mistaken, can only multiply, divide, add and subtract floating point numbers in decimal notation. If that is indeed so, the comparison with eval does not really seem justified to me.
Is there really a need, I wonder, for having an eval like function in Java ? The example of evaluating 2*2*2*2 does not really make this clear because you might just as well print this out directly. Perhaps a less trivial example would put this in better perspective ?
The possibility is to be able to load it directly to the array, not exclusively by source code and perform the operations directly. I understand that it is not the exact version but the concept is similar. The need is born of existence... now it already exists and it is possible that someone can help you. All the best! Thanks for the congratulations.
Java doesn’t have an eval direct equivalent, but it does have 2-way inter working with JavaScript and other scripting systems.
It’s easy to pass a String containing arbitrary JavaScript code and get back the result of executing it. It sounds like it may be inefficient but my experience is that any overhead is undetectable unless you are in a massive multi-user situation.
If you’re interested, google “Java ScriptEngine” for reference info, examples, and tutorials.

New Threads

Buy us a coffee!

Top Bottom