KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CustFunc


1 import org.nfunk.jep.*;
2
3 /**
4  * An example class to test custom functions with JEP.
5  */

6 class CustFunc {
7     
8     /**
9      * Constructor.
10      */

11     public CustFunc() {
12
13     }
14
15     /**
16      * Main method. Create a new JEP object and parse an example expression
17      * that uses the SquareRoot function.
18      */

19     public static void main(String JavaDoc args[]) {
20         
21         JEP parser = new JEP(); // Create a new parser
22
String JavaDoc expr = "1 + half(2)";
23         double value;
24         
25         System.out.println("Starting CustFunc...");
26         parser.addStandardFunctions();
27         parser.addStandardConstants();
28         parser.addFunction("half", new Half()); // Add the custom function
29

30         parser.parseExpression(expr); // Parse the expression
31
if (parser.hasError()) {
32             System.out.println("Error while parsing");
33             System.out.println(parser.getErrorInfo());
34             return;
35         }
36         
37         value = parser.getValue(); // Get the value
38
if (parser.hasError()) {
39             System.out.println("Error during evaluation");
40             System.out.println(parser.getErrorInfo());
41             return;
42         }
43         
44         System.out.println(expr + " = " + value); // Print value
45
}
46 }
47
Popular Tags