addition calculator from user input

I asked before but now I’m realizing some things I haven’t before. Ideally, if a user entered something as absurd as " 1.232 +     a3" I would want everything unnecessary like the spaces and letters to be parsed out and leave in the numbers (up to two decimal places is all I need). I tried to clean up the string using

[lua]selfImput1.text = string.gsub(stringInput.text, “[%s+%a+%p+]”, “”) [/lua] but I would need it to leave the decimal points alone and not to delete the “+” symbol.

After that, I was told that I can capture the values in the string using something like this:

[lua] leftOperand, operator, rightOperand = string:match(“10 + 20”, “(%d+)(%x)(%d+)”) [/lua]

And I’m assuming at this point I just add together the left and right operands since I’m only allowing addition to be used. 

Here’s a link to my other post. Someone else also posted a table to clean the characters but I am very unfamiliar with tables and feel I am only guessing what each line means.

https://developer.coronalabs.com/forum/2013/01/21/it-possible-allow-user-calculate-my-textfield

Thanks for any help!

Hi Nate,

So basically, you want to add the numbers, but you want to make sure that they’re numbers, i.e. if the user enters “a3@”, you want to just get “3” from this? If so, I would suggest that you first parse out your two sides (around the + sign), and you get basically everything (don’t exclude punctuation or anything). Then, once you have those two sides, do another string sub and parse out everything except the numbers part. Once you have those, do the calculation.

Further, before the addition step, you should convert them to numbers using the Lua “tonumber()” function. It will return “false” if the parameter can not be converted to a number. So, check that each side is “true” on that, and then add them up.

Brent

Thanks for the help Brent! But how can I go about parsing on either side of the “+” sign? On the whole, I experimented with the [^set] (I think I tried [^%+]) but I wasn’t able to get it to exclude the “+” sign. 

Also the other problem is the decimal places in the final string.match. When I use [lua]

  1. leftOperand, operator, rightOperand = string:match(“10 + 20”, “(%d+)(%x)(%d+)”) 

[/lua]

then it interprets the string “1.23 + 10” it only sees 23 and 10. I very briefly read on using “?” for things that may or not be there so I came up with: (%d%.?%d?%d?%d?) which can take up to 3 decimal places. I haven’t tested it yet but I’m thinking it should do the trick. Does that seem right to your eyes?

Hi Nate,

This should do the trick. For the left and right sides, I specify a “set” to match on, including a decimal point (but prefixed with a % since an explicit dot needs to be “escaped” in string matches).

[lua]

local leftOperand, operator, rightOperand = string.match(“1.23 + 10.111”, “([%d%.]+)([%s%p]+)([%d%.]+)”)

print(leftOperand, operator, rightOperand)

[/lua]

Note, however, that you’ll still need to parse the “operator” part. This sample gathers all punctuation and spaces, so you’ll need to further trim that down to only checking for logical math operators like +, -, /, *. I’m not sure how complex your calculator is, but just note this.

Hope this helps,

Brent

Hi Nate,

So basically, you want to add the numbers, but you want to make sure that they’re numbers, i.e. if the user enters “a3@”, you want to just get “3” from this? If so, I would suggest that you first parse out your two sides (around the + sign), and you get basically everything (don’t exclude punctuation or anything). Then, once you have those two sides, do another string sub and parse out everything except the numbers part. Once you have those, do the calculation.

Further, before the addition step, you should convert them to numbers using the Lua “tonumber()” function. It will return “false” if the parameter can not be converted to a number. So, check that each side is “true” on that, and then add them up.

Brent

Thanks for the help Brent! But how can I go about parsing on either side of the “+” sign? On the whole, I experimented with the [^set] (I think I tried [^%+]) but I wasn’t able to get it to exclude the “+” sign. 

Also the other problem is the decimal places in the final string.match. When I use [lua]

  1. leftOperand, operator, rightOperand = string:match(“10 + 20”, “(%d+)(%x)(%d+)”) 

[/lua]

then it interprets the string “1.23 + 10” it only sees 23 and 10. I very briefly read on using “?” for things that may or not be there so I came up with: (%d%.?%d?%d?%d?) which can take up to 3 decimal places. I haven’t tested it yet but I’m thinking it should do the trick. Does that seem right to your eyes?

Hi Nate,

This should do the trick. For the left and right sides, I specify a “set” to match on, including a decimal point (but prefixed with a % since an explicit dot needs to be “escaped” in string matches).

[lua]

local leftOperand, operator, rightOperand = string.match(“1.23 + 10.111”, “([%d%.]+)([%s%p]+)([%d%.]+)”)

print(leftOperand, operator, rightOperand)

[/lua]

Note, however, that you’ll still need to parse the “operator” part. This sample gathers all punctuation and spaces, so you’ll need to further trim that down to only checking for logical math operators like +, -, /, *. I’m not sure how complex your calculator is, but just note this.

Hope this helps,

Brent