• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this possible in Java? Execute a string formula with variables

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given a string pseudocode,

int y = 3;
String formula = "7*(y + 3)";
int answer = evaluate the formula substituting the value of 3 from y

is it possible to get the answer variable?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In my opinion, the easiest way to accomplish this is to use the Java Scripting API (see https://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/ for more details).

The API (along with a basic JS engine) is also built-in to Java, so there is no need to install anything.

Henry
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also look into the way compilers work to analyze program statement text to create program steps.

A deeper, slower, but fun exercise.

Bill
 
reply
    Bookmark Topic Watch Topic
  • New Topic