Simple Math Function

zyz0848

New Member
I'm completely inexperienced at programming, and I've been struggling to create a simple math function.

The function I'm trying to make is the following: vertical = squat*18.33/weight, where squat and weight are textviews, and vertical is a label.

For some reason, even though I enter values into my textviews, the label returns "NaN." Can someone explain why that happens to me?

I believe I have attached my code to this thread.

Also, I would greatly appreciate it if someone could recommend me simple tutorials, keeping in mind that I have zero programming experience.
 

Attachments

  • Vertical Calculator.zip
    7.2 KB · Views: 151

Vader

Well-Known Member
Licensed User
Longtime User
I guess without looking at your code that you are attempting to use the EditText's as numbers.

The following is a bad example, but it works and may be enough to help you.

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Public WeightText As EditText
   Public SquatText As EditText
   
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("Main")
   
   Private Vertical As Float = 0
   Private Weight As Float = 0
   Private Squat As Float = 0
   
   WeightText.Initialize("WeightText")
   SquatText.Initialize("SquatText")
   
   WeightText.text = "100"
   SquatText.Text = "10"
   
   Weight = WeightText.Text
   Squat = SquatText.text
   
   Vertical = (Squat *18.33)/Weight
   
   Log("Vertical = " & Vertical)

Dave
 
Upvote 0

bluejay

Active Member
Licensed User
Longtime User
You have not assigned any values to Number1 and Number2.

Try:
B4X:
Sub Enter_Click
    Number1 = Weight.Text
   Number2 = Squat.text
   lblComment.Text = Number1*18.33/Number2
End Sub

Also note that in the above example the words Public and Private can be removed since Activity variables are always private. The Private keyword is useful in Process_Globals since process variables are all Public.

Tip: in the designer set the edit boxes to input type = DECIMAL NUMBERS

bluejay
 
Upvote 0

zyz0848

New Member

Thank you! Your solution worked perfectly!
 
Upvote 0

derez

Expert
Licensed User
Longtime User
They are the same !
Actually it can also be
B4X:
Vertical = (Squat.Text *18.33)/Weight.Text
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…