• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Evaluating string expressions

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am looking out for something very urgently
The problem
I have a string "((45 > 21)&& (45 < 61)) || ((47 > 25) && (47< 51))"
this is dinamically created. I have gotta evaluate this string to return a boolean value of true or false as per the condition.
The problem is since it is a string i am not able to get it into an of condition but if i have the same expression as a boolean for eg boolean tbExp=((45 > 21)&& (45 < 61)) || ((47 > 25) && (47< 51));
then the compiler automatically assigns a value of true or false to tbExp as per the expression. Is there any way that i could convert a string object to a boolean. Otherwise how do i go about the same.
regards
riaz
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it your homework ? ;-)
As I known there isn't such ready library to do it in standard Java. (maybe regular
expressions can help)
So you have to write your lexical and grammer
parser,
write it from the ground, or use some
existed "lex/yacc like" tools.
Good luck !
Igor
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
riaz,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at Bean Shell
 
Riaz Mohamed
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
Thanks for poting your reply'w . I just happened to write a simple program and used reflectiion to get what i needed. I'm pasting below what i had written an could be of a lotta use.
import java.io.*;
import java.lang.reflect.*;
public class expression
{
public static void main (String []arg)
{
//String strin="((45 > 21) && (45 < 61)) || ((47 > 25) && (47< 51))";
//Assuming u have a string as follows
String strin="((45 < 21) && (45 < 61)) || ((47 > 25) && (47< 51))";
//Creates a temporary file called Samp and assigns the string value toa boolean
String fileContent="package .net; \n"
+"public class Samp {\n "
+" boolean X = "+strin+";\n "
+" public boolean getValue() "
+" { \n"
+" return X;\n"
+" }\n"
+" }";
String args[] = { "-classpath", ".",
"-sourcepath", ".",
"-d", ".",
//"-verbose",
"Samp.java"};
BufferedWriter wr = null;
try
{
// write the file to the disc
wr = new BufferedWriter(new FileWriter("Samp.java"));
wr.write(fileContent);
wr.close();
//Code for compilation
boolean x = new sun.tools.javac.Main(System.out,"javac").compile(args);
//Use reflection to get the class that has been generated
Class c = Class.forName("Samp");
//Get an object of the class
Object o = c.newInstance();
//Invoke a method on the object
Method m = c.getDeclaredMethod("getValue", null);
String tsReturn=m.invoke(o,null).toString();
System.out.println(m.invoke(o,null));
System.out.println(tsReturn);
boolean returnBool=Boolean.valueOf(tsReturn).booleanValue();
System.out.println(returnBool);

}
catch(Exception e)
{
}
}
}
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me - it is very good solution,
but only for learning/hackers tasks.
(use compiler for evalute the string, it likes
use microscope instead hammer, but... very good
hackers solution of course)

It's forbidden to use it in serious products:
1. It can't work with only JRE (without java compiler).
2. What if there is error in the string expression ?
3. Java compiler use a lot of memory and cpu.
[ November 13, 2002: Message edited by: Igor Ko ]
 
I child proofed my house but they still get in. Distract them with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic