Android Question Stupid Question about dividing 2 values.

I know this question will seem stupid for all of you whom are familiar with the coding here. I used do a lot of calculators in VB. I just want to perform calculations on 2 numbers placed into edittext boxes named Voltage and Amperage then place the result in a edittext box named Resistance. I also want to trap errors by verifying that a number was placed in the Voltage and Amperage textboxs prior to running the calculations.

Any help would be sincerely appreciated.

Current Code gets errors on both the error traps and Resistance = Voltage / Amperage

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.

End Sub

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

Private Button1 As Button
Dim Amperage As Double
Dim Resistance As Double
Dim Voltage As Double

End Sub

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

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
'If Voltage_TextChanged = "" Then
'Msgbox("You must enter a voltage first","oops")
'End If
'If Amperage_TextChanged = "" Then
'Msgbox("You must enter an amperage first","oops")
'End If

Resistance = Voltage / Amperage
End Sub
Sub Voltage_TextChanged (Old As String, New As String)

End Sub
Sub Resistance_TextChanged (Old As String, New As String)

End Sub
Sub Amperage_TextChanged (Old As String, New As String)

End Sub
 

KMatle

Expert
Licensed User
Longtime User
Hi and welcome,

first: use code tags (I will be displayed much more readable) and always post the error message(s) from the log.

What I can see is that you haven't understood what views are and how they work. Take a look at the beginners guide here: http://www.b4x.com/android/forum/threads/beginners-guide.9578/

To help you right now:

1. The declaration of the Edittext's are missing (should be like Button1 -> Private Edittext1 as Edittext)
2. What are the names of the Edittext's? You may not use the same name for them as another variable (f.e. Amperage is a variable OR an Edittext)
3. To get the content of a Edittext you use: Amperage = Edittext1.Text
4. You could name the variable amp and the Edittext AmpET -> amp = AmpET.Text
5. To check a valid value you can change the input_type of the Edittext in the designer to decimal numbers.
6. In "Button1_Click" check the value by "If AmpET.Text.length > 0 then ..."
 
Upvote 0
Hi and welcome,

first: use code tags (I will be displayed much more readable) and always post the error message(s) from the log.

What I can see is that you haven't understood what views are and how they work. Take a look at the beginners guide here: http://www.b4x.com/android/forum/threads/beginners-guide.9578/

To help you right now:

1. The declaration of the Edittext's are missing (should be like Button1 -> Private Edittext1 as Edittext)
2. What are the names of the Edittext's? You may not use the same name for them as another variable (f.e. Amperage is a variable OR an Edittext)
3. To get the content of a Edittext you use: Amperage = Edittext1.Text
4. You could name the variable amp and the Edittext AmpET -> amp = AmpET.Text
5. To check a valid value you can change the input_type of the Edittext in the designer to decimal numbers.
6. In "Button1_Click" check the value by "If AmpET.Text.length > 0 then ..."

Thank you so very much I had watched most of the videos and saw no real reference to what I needed. I was not aware of the books.

I was also not aware that I had to declare Amperage = Edittext1.Text since I renamed it Amperage I though I could call it out by the new name.


Again thanks a million.

 
Upvote 0
Ok, so I renamed the text boxes Amperage_txt and Voltage_txt. I am able to convert them to int, but when I set it up to fetch the data the name I give them show in red. Also the btn click where I attempt to do the calculation also shows in red. Where did I go wrong?

Sub Process_Globals

End Sub
Sub Globals
PrivateAmperage_txtAsEditText
PrivateCalc_btnAsButton
PrivateVoltage_txtAsEditText
DimAmperage_txt, Voltage_txtAsInt

End Sub
Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("first")

End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed AsBoolean)
End Sub
Sub Button1_Click
IfVoltage_txt.length > 0Then
Msgbox("You must enter a voltage first","oops")
EndIf
IfAmperage_txt.length > 0Then
Msgbox("You must enter an amperage first","oops")
EndIf
:mad:Amps = Amperage_txt.Text
:mad:Volts = Voltage_txt.Text
:mad:Resistance.lbl = Volts / Amps
 
Upvote 0
Sorry forgot the errors.

Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'resistance' is used before it was assigned any value.
Occurred on line: 49
Resistance.lbl = Volts / Amps
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Process_Globals

You are STILL posting Code WITHOUT using Code-tags!And you get told to do so...
first: use code tags (I will be displayed much more readable)

If you want to break the rules and ignore such comments; i´ll ignore your posts then in future :-/

codetag001.png


codetag002.png


codetag003.png

results in:
B4X:
dim a as string
 
Upvote 0
I apologize for the error but I did not see how to insert code. When I hovered over the button it just said insert. I thought it was for a file. My mistake. That is why I zipped the file and uploaded it to my webserver and posted a link. I apologize again as I have never had to use a bulletin board prior to this.

Thanks to Klaus, I was able to scrap the code and start over and leave everything with the provided names and it seemed ok, but the emulator showed it without the text boxes being editable. So I tried to change them to common names as Klaus suggested but I keep getting errors on the compile.

Here is the code in the correct format I hope.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

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

    Private Button1 As Button
    Private Button2 As Button
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Page1")
    End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub EditText3_TextChanged (Old As String, New As String)
   
End Sub
Sub EditText2_TextChanged (Old As String, New As String)
   
End Sub
Sub EditText1_TextChanged (Old As String, New As String)
   
End Sub
Sub Button2_Click
   
End Sub
Sub Button1_Click
    If EditText1.length > 0 Then
    Msgbox("You must enter a voltage first","oops")
    End If
    If EditText2.length > 0 Then
    Msgbox("You must enter an amperage first","oops")
    End If   
    EditText1.Text = Volts
    EditText2.Text = Amps
    EditText3.Text = Ohms
    Dim Volts, Amps, Ohms As Int
    Ohms = Volts / Amps
End Sub

This is the error shown on the compiler debugger.

Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'volts' is used before it was assigned any value.
Occurred on line: 66
EditText1.Text = Volts
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You shouldn't check for the validity of .text values by calling the Edittextbox_textchanged event(!)

B4X:
 if amp.text = null or amp.text="" then
amp.text not valid! Do something!
Else if volt.text...

Shouldn't be too hard to adjust to your needs
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I am guessing the order of this code is wrong:
B4X:
   EditText1.Text = Volts
    EditText2.Text = Amps
    EditText3.Text = Ohms
    Dim Volts, Amps, Ohms As Int
    Ohms = Volts / Amps

I would rewrite it as:
B4X:
   Dim Volts, Amps, Ohms As Int
   Volts = EditText1.Text
   Amps = EditText2.Text
   Ohms = Volts / Amps
   EditText3.Text = Ohms

As a sidenote, I am not sure if you want Volts/Amps/Ohms to always be integers.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
upload_2014-11-10_13-29-25.png



(Set Input Type = DECIMAL_NUMBERS for edtVoltage and edtAmperage)

Use a label (lblResistance) for the result.

B4X:
Sub edtAmperage_TextChanged (Old As String, New As String)
    btnCalculate.Enabled = (edtAmperage.Text.Length > 0) AND (edtVoltage.Text.Length > 0)
End Sub

Sub edtVoltage_TextChanged (Old As String, New As String)
    btnCalculate.Enabled = (edtAmperage.Text.Length > 0) AND (edtVoltage.Text.Length > 0)
End Sub

Sub btnCalculate_Click
    Dim Volt As Float = edtVoltage.Text
    Dim Amp As Float = edtAmperage.Text
    Dim Res As Float = Volt / Amp
    lblResistance.text = Res
End Sub
 
Upvote 0
I truly, deeply and sincerely appreciate every ones help. I had used integer because I saw it in someone else's code. LucaMs's code worked perfectly after I changed to his naming conventions and pasted his code into the program. So I should always use a label for the result then? Do I have to import a library to call a square root function?

Once again thanks to everyone for there help.
 
Upvote 0
Well thanks to all of your great assistance I was able to get the first of many calculators done. Believe when I say that I could not have done it without your help and support so thanks again.

Below is an image file of the app in the emulator.

Also thank you for the Sqrt info.
 

Attachments

  • Calc..jpg
    Calc..jpg
    353.4 KB · Views: 188
Upvote 0
Top