Simple math error using "textField"

Hi guys!

I’m having an error in a “text field” when I try to multiply “event.target.text” with any number.

Here is the code

local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local background local numericField\_1 local math\_1 local result = 0 local myText -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- local function backgroundTap( event ) native.setKeyboardFocus( nil ) return true end local function textListener( event ) if ( event.phase == "began" ) then print( "user begins editing numericField\_1" ) elseif ( event.phase == "ended" or event.phase == "submitted" ) then --Output resulting text from "defaultField" --print( event.target.text ) if event.target.text then math\_1 = event.target.text \* 10 -- line 40 print( math\_1 ) myText.text = math\_1 elseif ( event.target.text == "" ) then math\_1 = nil end return true elseif ( event.phase == "editing" ) then print( event.newCharacters ) print( event.oldText ) print( event.startPosition ) print( event.text ) end end

And in “scene: create”

-- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen background = display.newImageRect( "images/backgroundimg.jpg", 768, 1024 ) background.x = centerX background.y = centerY sceneGroup:insert( background ) background:addEventListener( "tap", backgroundTap ) numericField\_1 = native.newTextField( centerX-65, centerY-320, 250, 60 ) numericField\_1:setTextColor( 0.8, 0.8, 0.8 ) numericField\_1.hasBackground = false --numericField\_1.text = "enter value!" numericField\_1:addEventListener( "userInput", textListener ) myText = display.newText( result, 535, 200, native.systemFont, 100 ) end

I can do the math “math_1” correctly and “myText” is replaced on screen by “math_1” but if the “textField” is empty and I touch the background, which already has a “native.setKeyboardFocus (nil)” function, the simulator gives me the following error.

lua:40 = math_1 = event.target.text * 10
 

Hi @dodi_games,

This is an easy fix. Even though you might only be typing in numbers, they come through (from the text input) as a string value, as far as Lua is concerned. Clearly Lua can’t do math between a string and a number, so you need to both check and convert the inputted value to a number before doing math on it, like this:

[lua]
if tonumber( event.target.text ) then

   math_1 = tonumber( event.target.text ) * 10 – line 40

end

[/lua]

Brent

I know that is the correct way to do it Brent, but I thought that Lua automatically casts from string to int when needed?

For example if I do this:

local myNum = "10" + 5 print("myNum = ", myNum)

it prints 15 rather than throwing an error.

quick drive by:  wrap it in an if test to check for nil (or blank string if prior to tonumber()).  no math on nil ever, period.

@alan:  tonumber("") == nil – equivalent to auto coercion result

Hi @dodi_games,

This is an easy fix. Even though you might only be typing in numbers, they come through (from the text input) as a string value, as far as Lua is concerned. Clearly Lua can’t do math between a string and a number, so you need to both check and convert the inputted value to a number before doing math on it, like this:

[lua]
if tonumber( event.target.text ) then

   math_1 = tonumber( event.target.text ) * 10 – line 40

end

[/lua]

Brent

I know that is the correct way to do it Brent, but I thought that Lua automatically casts from string to int when needed?

For example if I do this:

local myNum = "10" + 5 print("myNum = ", myNum)

it prints 15 rather than throwing an error.

quick drive by:  wrap it in an if test to check for nil (or blank string if prior to tonumber()).  no math on nil ever, period.

@alan:  tonumber("") == nil – equivalent to auto coercion result