String Conversion

Posted on 2004-11-09
Last Modified: 2010-05-18
Is there an easy way to covert String let’s say “x + y”? So that, I could use it as an equation. For example,

text.getText(); // x + y
z = x + y;

Assume that I have declared x and y.

Thank you
Question by:musicc
    LVL 15

    Expert Comment


    Java is a compiled language, and once a program have been compiled it cannot easily understand such 'human readable' expressions.

    In scripting languages like Basic, Perl, PHP and JavaScript it can be done easily as those languages are not compiled, instead they are run by being incrementally compiled and executed. The compiler is always present and it is no trouble to have it interpret and execute the string "x + y".

    That does not mean it is impossible to write a java program to read and interpret a string like "x + y", it is a stock project for first or second year computing classes.

    But not exactly easy (last time I made such a program it ran to about 200 lines of code).

    regards JakobA
    LVL 1

    Expert Comment

    There are interpretters that will do exactly what your looking for.

    One is called Bean Shell for Java:

    I've also developed one that I am selling.  First you define your variables and functions in a set of libraries, then you can run the expressions as a string as you are suggesting.

    Author Comment

    I found one. It is very useful.
    Here is the site.
    LVL 1

    Expert Comment

    JakobA gave him the answer, and I gave him the best work around, I believe the points should be split.
    LVL 15

    Expert Comment

    Not for me please.

    Accepted Solution

    PAQed with points refunded (50)

    Community Support Moderator

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This video teaches viewers about errors in exception handling.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now