?
Solved

String Conversion

Posted on 2004-11-09
7
Medium Priority
?
188 Views
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
0
Comment
Question by:musicc
6 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 12539560
No.

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
0
 
LVL 1

Expert Comment

by:wgilster
ID: 12548801
There are interpretters that will do exactly what your looking for.

One is called Bean Shell for Java:
http://www.beanshell.org/

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.
0
 

Author Comment

by:musicc
ID: 12579080
I found one. It is very useful.
Here is the site.
http://www.singularsys.com/jep/doc/html/index.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:wgilster
ID: 12796353
JakobA gave him the answer, and I gave him the best work around, I believe the points should be split.
0
 
LVL 15

Expert Comment

by:JakobA
ID: 12796754
Not for me please.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12828016
PAQed with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 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