JUEL mathematics

638 Views Asked by At

I'm programming server on java, and I need to use math with the help of JUEL.

For example 2*2+2 and it will print out the result 6. The problem is, it doesn't work.

Here is a method for mathematics:

public static void Math(String operation){

    ValueExpression expr = f.createValueExpression(cont, "${" + operation + "}", Object.class);
    out.print(expr);

}

So, I take an operation as String "8+5+4"and print out result on conlsole.

May be I use JUEL in a wrong way?

try{
        while ((newLine = in.readLine()) != null) {
            try {
               Pattern patternMath = Pattern.compile("<MATH>(\\s*?)(\\d+)(\\s*?)([-+/\\*])(\\s*?)(\\d+)");
               Matcher matcher = patternMath.matcher(newLine);

                Pattern pattCSEN = Pattern.compile("<RU-EN>(.*)");
                Matcher matCSEN = pattCSEN.matcher(newLine);

                Pattern pattENCS = Pattern.compile("<EN-RU>(.*)");
                Matcher matENCS = pattENCS.matcher(newLine);




                if (matENCS.find()) {out.println("<TRANSLATION> " + ENGtoCZ(matENCS.group(1)));}
                else if (matCSEN.find()) {out.println("<TRANSLATION> " + CZtoENG(matCSEN.group(1)));}
               else if (newLine.equals("<BYE>")) {
                    clientSocket.close();

                }
                else  if (matcher.find()) {

               Math(newLine);

                } else {
                    out.println("<FAIL>");
                    clientSocket.close();

                }
            }catch(Exception ex){
                clientSocket.close();;
            }}
            } catch (Exception e) {
                out.println("<FAIL>");
                clientSocket.close();
            }
        }
1

There are 1 best solutions below

0
Nirmal On BEST ANSWER

Here are the following lines to always make sure that you have in your code. Make sure you have the context set up something like the following:

SimpleContext context = new SimpleContext();

You expression object should be of the Implemented class, something like the following:

ExpressionFactory factory = new ExpressionFactoryImpl();

JUEL Api

Secondly put your ValueExpression in a try block, in case anything goes wrong, you will know the problem from the caught exception.

Check how expression is entered with a System.out as in:

Object display = expr.getValue(context);
System.out.println("> " + display);

Some additional reference is Here