1

I have users entering numbers into an EditText fields, but what I would like them to be able to enter is 100+32+10 into the fields as well. I expect simple expressions only, common four functions. In fact, just addition would cut it too.

For just addition, I guess it wouldn't be that hard to split up the string and do it myself I suppose, but I'm curious to know if there is a library which can do what I want much more cleanly.

Any suggestions?

Patrick W. McMahon
  • 3,488
  • 1
  • 20
  • 31
capcom
  • 3,257
  • 12
  • 40
  • 50
  • 1
    If you are only doing addition, I would just use `string.split()`. It will save the hassle of importing an entire library just for this. If you intend to expand to other functions as well, you will want to either look into regex or one of the libraries down below. – Cat Aug 16 '12 at 19:23
  • @Eric, I think you are right. Split seems like easiest way. Thanks. – capcom Aug 16 '12 at 19:41

2 Answers2

2

There are a ton of Java expression libraries.

I've used JEXL on Android with no issues, but there are plenty of other options.

A full-on EL might be more than you need; essentially everybody in the world has written simple expression parsers that might be adequate for your needs, or roll a quick one yourself.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

Just to let everyone know, I solved my problem by writing these few lines of code:

public float calculateBillAmount(String expression){

    String delim = "\\+";
    String[] stringTokens = expression.split(delim);
    float result = 0;

    for (int i = 0; i < stringTokens.length; i++)   {
        result += Float.parseFloat(stringTokens[i]);
    }

    return result;
}
capcom
  • 3,257
  • 12
  • 40
  • 50