Android Question edittext to number

Richard P

Member
Licensed User
Longtime User
Mr beginner again

I have gone back to basics after my previous failed attempts, although i have coded my layout.
My question;

I have two user entries; Litres as edittext and miles as edittext
I want to take these two entries and [1] convert litres to gallons using a factor 4.45 and [2] calculate miles per gallon

when i try and calculate the mpg = miles/gallons i get a 'numberformat - invalid double error

What am i missing?

The factor, ltr, gal, mls etc have been dim'd in sub_globals hence the "'"

litres_EnterPressed
'Dim factor As Float 'Sets up conversion factor and converts to gallons
factor = 4.45
'Dim ltr As Float
ltr = "litres"
'Dim gal As Float
gal = (litres/factor)

End Sub

Sub miles_EnterPressed
'Dim mls As float ' Sets miles as integer'
mls = miles

End Sub


Sub calc_click 'Calculates the mpg and displays in label answer'
Dim result As Int
result = ("mls"/"gal")
answer.Text = "Your miles per gallon was " & result

End Sub


Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

klaus

Expert
Licensed User
Longtime User
You should declare all your variables as Double not Float nor Int.
And in the calculation routine use
result = mls / gal
no quotes, you are using numeric varaibles !
You can use NumberFormat or NumberFormat2 to format the result output.
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
Thank you Klaus,

I think i have made the main changes as suggested however still getting the same error at; gal = litres / factor.

I have not used the 'numberformat' yet as i don't quite understand the format.

Richard

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim title As Label
Dim litres As EditText
Dim miles As EditText
Dim calc As Button
Dim answer As Label

Dim factor As Double 'Variables for calculation'
Dim ltr As Double
Dim gal As Double
Dim mls As Double

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")

Activity.Initialize("mpgcalc")

title.Initialize("") 'Title set up'
title.Color=Colors.Yellow
title.TextSize=25
title.TextColor=Colors.Black
title.Gravity=Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
title.Text="MPG CALCULATOR"

litres.Initialize("litres") 'litres user entry EDITTEXT'
litres.Color=Colors.White
litres.TextSize=25
litres.TextColor=Colors.Black
litres.Gravity=Gravity.LEFT
litres.InputType = litres.INPUT_TYPE_NUMBERS

miles.Initialize("miles") 'miles user entry EDITTEXT'
miles.Color=Colors.White
miles.TextSize=25
miles.TextColor=Colors.Black
miles.Gravity=Gravity.LEFT
miles.InputType = miles.INPUT_TYPE_NUMBERS

calc.initialize("calc") 'Set up button to start calculation'
calc.Color=Colors.Yellow
calc.TextSize=35
calc.TextColor=Colors.Black
calc.Gravity=Gravity.CENTER_HORIZONTAL + Gravity.CENTER_VERTICAL
calc.Text = "Click to calculate MPG"

answer.initialize("answer") 'Set up label answer for result'
answer.color=Colors.White
answer.TextSize=25
answer.TextColor=Colors.Black
answer.Gravity=Gravity.LEFT


'initialize.layout(mpgcalc.bal) 'Create the view'
Activity.AddView(title,20%x, 10dip, 60%x, 45dip)
Activity.AddView(litres, 250dip, 100dip, 100dip, 40dip)
Activity.AddView(miles, 250dip, 200dip, 100dip, 40dip)
Activity.AddView(calc, 250dip, 350dip, 400dip, 90dip)
Activity.AddView(answer,250dip, 500dip, 200dip, 40dip)


End Sub

Sub litres_EnterPressed 'Sets up conversion factor and converts to gallons
factor = 4.45
'ltr = litres
gal = litres / factor

End Sub

Sub miles_EnterPressed ' Sets miles as integer'
mls = miles

End Sub


Sub calc_click 'Calculates the mpg and displays in label answer'
Dim result As Double
result = mls / gal
answer.Text = "Your miles per gallon was " & result

End Sub


Sub Activity_Resume


End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Hi Richard .. try to use [ Code ] Paste your code here .. [/Code] (no spaces in first code tag)

You did not make reference to the Text property of your "litres" and "miles" Edit Texts ...
adjust your subs as such.

B4X:
Sub litres_EnterPressed 'Sets up conversion factor and converts to gallons

   factor = 4.45
   ltr = litres.Text  'assign the content of EditText "Litres" to ltr variable

   gal = ltr / factor

End Sub

Sub miles_EnterPressed

   mls = miles.Text     'assign the content of EditText "miles" to mls variable

End Sub
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
Thank you for the help. have reviewed and added the amendments suggested by yourself and Klaus and have at last completed my first working app (Christmas has come early).

I will now develop the app a bit more to learn some more techniques and code, before moving onto another more complex app.

For future reference not sure how I insert code as per your reply?

Once again thank you for the help.

Richard
 
Upvote 0

Richard P

Member
Licensed User
Longtime User
You should declare all your variables as Double not Float nor Int.
And in the calculation routine use
result = mls / gal
no quotes, you are using numeric varaibles !
You can use NumberFormat or NumberFormat2 to format the result output.

Thank you for the advice - have my first working app

Little steps but progress.

Richard
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Richard ... glad to see youve made progress.

Regarding posting sections of code .. one method is to use the start code / end code tags

Manual Code Tags.JPG


Results in this...
B4X:
copy & paste all your code here ....


There is a also a shortcut in a new post toolbar to do this ...
InsertCode.jpg
 
Last edited:
Upvote 0
Top