Results 1 to 7 of 7

Thread: The eval() method in Java

  1. #1
    Join Date
    Mar 2010
    Posts
    372

    The eval() method in Java

    Hello,
    I want a method that works like
    Code:
    eval (exp) ...
    For example, if we eval ("test" + "1" + ". Sleep ()") perform temp.dort (). I guess you can understanding what I am trying to say. Please help me with this. Also, an example code will be very helpful for me. thank you.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: The eval() method in Java

    In java, there is a method eval (String) that evaluates a string as the name of a method, a calculation or something else.
    For example:
    Code:
    alert (eval ("3 +4")); displays the alert box containing 7
    But java is not as easy to evaluate a calculation entered by the user for example, must be created or even a method that parses the string and that the calculation result. Here are some tips to help you make a method like this. There are several ways to do this but here is one (I do not know if this is the best):
    Code:
    Create a method (called eval example): 
    public double eval (String s) (
    / / Content here
    )
    Then, a first part of this method takes the contents of the bracket and replace it with his monogram. To isolate the parentheses you can use a split or a substring with indexOf. Let's say you insulate your way of parenthesis that txtAvt contains what is before the parenthesis, txtApres there after and parenthesis the content of the parenthesis.
    You can then do:
    Code:
    return eval (sometxt + eval (parenthesis). toString () + txtApres)
    After that (if there is no parenthesis method continues to run until you get on another return) must be cut string (with a split) that the sums and differences: Indeed, if we calculate 3 / 2 5 * 3 must first be 3 / 2 | (5 * 3)
    If you have a String [] sum which contains eg all are doing a loop that adds eval (sum [i]) to a variable (result, for example) and variable return (result) at the end of the loop:
    Code:
    double result = 0;
    for (int i = 0; i <temp.length i + +) (
    result + = sum [i];
    )
    return result;
    Same process for multiplication and division. And the end of your method will be executed only if no more + - * / and () in your string then you can make a Double.parseDouble (s) if s is the string to convert. Remember to make a try (... ) Catch (NumberFormatException e) ... ( ) In order to prevent errors.


    Supplementary Note :
    Beware, the split with the views (*) and more + not working properly because the split takes into account the regular expressions. A simple solution is to
    Code:
    s.replace ('+',' P '). split ("P");
    instead of
    Code:
    s.split ("+");
    (But do not contain other characters s 'P'!) and
    Code:
    s.replace ('*',' x '). split ("x");
    instead of
    Code:
    s.split ("x");
    You can also make a s.replace (',','.'); that commas are correctly interpreted by Double.parseDouble (s)! (3.2345 dismissal if an error because you must write 3.2345)

    If you still have queries then you can follow this
    Eval function in javascript

  3. #3
    Join Date
    Mar 2010
    Posts
    184

    Re: The eval() method in Java

    Here is an example but it is in javascript:
    Code:
    b.value = eval ("test.prix" x.toString + () + ". value"); 
    x.tostring () is for prix1, prix2, prix3 ...
    I have used this code and I am sure that this code works for me, I think you should just give a try to it.

  4. #4
    Join Date
    Dec 2009
    Posts
    292

    Re: The eval() method in Java

    An eval () is realistic in an interpreted language, it is a nonsense in a compiled language (I recall that Java is compiled). However, since Java comes with everything a library of classes, the Java compiler itself (as it is written in Java), it is possible to generate a class, and to do compile (but it seems to me that the functions are undocumented. In this case, go dig into the sources of the JDK). Following is a compiled class as another.

  5. #5
    Join Date
    Sep 2009
    Posts
    125

    Re: The eval() method in Java

    you can always play with the reflection. But to where you want to call the method sleep () on object foo. Now you have no pointer to the object foo, foo unless a class is static and sleeps, the algorithm (assuming a class foo and sleeps static):
    s = "toto.dort ()"
    Class c = Class.forName (s.substring (0, s.indexOf ("."));
    Method m = c.getDeclaredMethod (s.substring (s.indexOf (".")), Null);
    m.invoke (null, null);

  6. #6
    janysek Guest

    Re: The eval() method in Java

    The eval() method accepts a string of JavaScript statements(method, expressions) and evaluates it as JavaScript source code.
    eval() returns the value of the executed JavaScript statement.

  7. #7
    Join Date
    Sep 2009
    Posts
    177

    Re: The eval() method in Java

    Even if an algorithm but not c coding , But that bp c foo in this case is an instance. In fact I want to do something like: I click on a button object is created dynamically, and has an identifier , so that I can use it later, of course, all this without table or list or vector.

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Method replaceAll in java
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 01:06 AM
  3. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  4. Problem with my java method
    By manjava in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 06:15 AM
  5. Java: How can I call one method in another method?
    By biohazard 76 in forum Software Development
    Replies: 3
    Last Post: 16-07-2009, 07:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,055,570.73872 seconds with 17 queries