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?

2
  • 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. Commented Aug 16, 2012 at 19:23
  • @Eric, I think you are right. Split seems like easiest way. Thanks. Commented Aug 16, 2012 at 19:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.