Search code examples
javaandroidscriptingbeanshell

how to make methods in beanshell?


I made a simple beanshell ide in android using an edittext and a button. When the button is clicked, Interpreter.eval() is called and edittext.getText().toString() is passed in as the parameter. I want to know: how can I make a method in beanshell and run it?

This is the code i m trying to execute in my beanshell ide:

import android.widget.Toast

int i=add(1, 5);
Toast.makeText(context, ""+i, 5000).show();

int add(int i, int j){
    return i+j;
}

But i get the following error:

Command not found: add()


Solution

  • Have you tried moving your function definition above its usage, like so;

    import android.widget.Toast
    
    int add(int i, int j){
        return i+j;
    }
    
    int i=add(1, 5);
    Toast.makeText(context, ""+i, 5000).show();
    

    Does that make any difference?