Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eval() Interprter return null

Tags:

java

android

I am working on Mobile app calcluator I am trying to evaluate a string as a math equation by using "bsh" library. Successfully, I imported the library and compiled.

When I try to implement this part under the equal sign I am getting null all the time

equSign.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String value = "";
              Double result= null;
                  Interpreter interpreter = new Interpreter();
              try {
                     result = (Double)interpreter.eval(input);
                  } catch (EvalError evalError) {
                      evalError.printStackTrace();
                  }
              value = String.valueOf(result);
              text.setText(value);
          }
      });

every time the user pree 0 to 9 numbers or . or any opertion + / - * it will be added to input variable

  Num0.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          text.setText(text.getText()+"0");
          input = input + "0";
      }
  });

this function for all other buttons

I used try catch as well to catch an error

like image 312
Hemm Avatar asked Apr 28 '26 01:04

Hemm


1 Answers

Your issue appears to be that you're mixing variables of your java class and your 'bsh' interpreter. Basically bsh cannot access your java variables directly and you need to set them in the interpreter.

i.eval(input) is probably what you meant to do.

Take a look at the examples in http://www.beanshell.org/javadoc/bsh/Interpreter.html to see how some other features are used.

like image 175
Kiskae Avatar answered Apr 30 '26 14:04

Kiskae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!