Android Question Calcolatrice / calculator

actarus63

Member
Licensed User
calcolatrice
buon pomeriggio stavo creando una calcolatrice dopo aver visto come spunto un tutorial. Quando vado lancio l'applicazione questa parte regolarmente; le operazione vanno bane; ma quando vado a fare l' operazione con la sottrazione , l'applicazione va in crash. dove sbaglio? grazie per l'aiuto.
ps qui di seguito la stringa dell'errore dove mi riporta la pagina del lgs

calculator
good afternoon I was creating a calculator after seeing a tutorial as a starting point. When I go launch the application this part regularly; the operations go but; but when I go to do the subtraction operation, the application crashes. where am I wrong? Thanks for your help.
ps below the string of the error where it brings me the page of the lgs

........
Dim hasil As String
Dim num1 As Int
Dim num2 As Int
.........

Sub ButtonMinus_Click
num1=EditText1.Text
EditText1.Text=""
hasil="-"
End Sub
……..

Sub ButtonEqual_Click
num2 = EditText1.Text
If hasil = "+" Then
EditText1.Text = num1 + num2
Else If hasil = "-" Then
EditText1.Text = num1 - num2
Else If hasil = "x" Then
EditText1.Text = num1 * num2
Else If hasil = "/" Then
EditText1.Text = num1 / num2
Else If hasil = "-" Then

End If
end sub
 

eurojam

Well-Known Member
Licensed User
Longtime User
Please use CODE Tags

2018_01_10_15_56_15_Android_Question_Calcolatrice_calculator_B4X_Community_Android_iOS_des.png
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi, in the code you submitted the "-" case is presented two times (first time you calculate a subtraction, second time you do nothing):
B4X:
Sub ButtonEqual_Click
num2 = EditText1.Text
If hasil = "+" Then
EditText1.Text = num1 + num2
Else If hasil = "-" Then
EditText1.Text = num1 - num2
Else If hasil = "x" Then
EditText1.Text = num1 * num2
Else If hasil = "/" Then
EditText1.Text = num1 / num2
Else If hasil = "-" Then

End If 
end sub

Can you report a complete error description (when your app crashes)?
 
Upvote 0

actarus63

Member
Licensed User
ciao udg, in effetti il codice "-" ho dimenticato di cancellarlo. Praticamente l'app,va in crash solo quando clicco i simboli matematici e cioè i segni : - ; + ; x ; / .
Se io vorrei fare un'operazione del tipo - 2 + 1 oppure +4-10 l'app va subito in crash, appena clicco il simbolo
B4X:
Private ButtonMinus As Button
------
Dim hasil As String
    Dim num1 As Int
    Dim num2 As Int
-------

Sub clearButton_Click
    EditText1.Text=""
    End Sub

Sub Buttontt_Click
    EditText1.Text = EditText1.Text & "."
End Sub

Sub ButtonMultiply_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="x"
    
End Sub

Sub ButtonMinus_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="-"
    
End Sub

Sub ButtonEqual_Click
    num2 = EditText1.Text
    If hasil = "+" Then
    EditText1.Text = num1 + num2
    
    Else If hasil = "-" Then
        EditText1.Text = num1 - num2
    
    Else If hasil = "x" Then
        EditText1.Text = num1 * num2
    
    Else If hasil = "/" Then
        EditText1.Text = num1 / num2
    
    End If   
End Sub
hi udg, in fact the code "-" I forgot to delete it. Basically, the app only crashes when I click the mathematical symbols, that is the signs: -; +; x; /.
If I want to do an operation like - 2 + 1 or + 4-10 the app immediately crashes, as soon as I click the symbol
 
Upvote 0

actarus63

Member
Licensed User
questo è il codice completi, devo solo aggiungere il %
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private EditText1 As EditText
    Private Button1 As Button
    Private Button10 As Button
    Private Button2 As Button
    Private Button3 As Button
    Private Button4 As Button
    Private Button5 As Button
    Private Button6 As Button
    Private Button7 As Button
    Private Button8 As Button
    Private Button9 As Button
    Private ButtonAdd As Button
    Private ButtonDevide As Button
    Private ButtonEqual As Button
    Private ButtonMinus As Button
    Private ButtonMultiply As Button
    Private Buttontt As Button
    Private calcolatriceLabel As Label
    Private clearButton As Button
    Private infoButton As Button
    
    Dim hasil As String
    Dim num1 As Int
    Dim num2 As Int
    
    Dim myImage As Bitmap
    
'    Dim stu As StringUtils
    
    Private backgroundImage As ImageView
    
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")
    
    myImage.Initialize(File.DirAssets, "infoButton.png")
    infoButton.SetBackgroundImage(myImage)
    
End Sub

Sub Activity_Resume
    
    

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub clearButton_Click
    EditText1.Text=""
    End Sub

Sub Buttontt_Click
    EditText1.Text = EditText1.Text & "."
End Sub

Sub ButtonMultiply_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="x"
    
End Sub

Sub ButtonMinus_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="-"
    
End Sub

Sub ButtonEqual_Click
    num2 = EditText1.Text
    If hasil = "+" Then
    EditText1.Text = num1 + num2
    
    Else If hasil = "-" Then
        EditText1.Text = num1 - num2
    
    Else If hasil = "x" Then
        EditText1.Text = num1 * num2
    
    Else If hasil = "/" Then
        EditText1.Text = num1 / num2
    
    End If   
End Sub

Sub ButtonDevide_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="/"
    
End Sub

Sub ButtonAdd_Click
    num1=EditText1.Text
    EditText1.Text=""
    hasil="+"
End Sub

Sub Button9_Click
    EditText1.Text = EditText1.Text & "9"
End Sub

Sub Button8_Click
    EditText1.Text = EditText1.Text & "8"
End Sub

Sub Button7_Click
    EditText1.Text = EditText1.Text & "7"
End Sub

Sub Button6_Click
    EditText1.Text = EditText1.Text & "6"
End Sub

Sub Button5_Click
    EditText1.Text = EditText1.Text & "5"
End Sub

Sub Button4_Click
    EditText1.Text = EditText1.Text & "4"
End Sub

Sub Button3_Click
    EditText1.Text = EditText1.Text & "3"
End Sub

Sub Button2_Click
    EditText1.Text = EditText1.Text & "2"
End Sub

Sub Button10_Click
    EditText1.Text = EditText1.Text & "0"
End Sub

Sub Button1_Click
    EditText1.Text = EditText1.Text & "1"
End Sub

Sub infoButto_Click
    StartActivity(Info)
    
End Sub

Sub infoButton_Click
    StartActivity(Info)
End Sub

this is the complete code, I just have to add the%
 
Upvote 0

actarus63

Member
Licensed User
non mi fa fare l'upload il server b4a

it does not make me upload the b4a server
 

Attachments

  • 2018-01-10 (1).png
    2018-01-10 (1).png
    10.2 KB · Views: 327
Upvote 0

udg

Expert
Licensed User
Longtime User
In the meanwhile, a clear problem with your code is the use of symbols plus (+) and minus (-) both for math operators and number signs.
What I mean is, when you have an expression like -2 + 1, pressing the minus button will execute an instruction where you try to convert EditText1 (which contains the caracter "-" or is empty, it depends on how you use the program) to a number.

Did you read the thread suggested by @lemonisdead on post #4 above?
Another helpful reading could be this one (in Italian) about RPN.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
RPN (Reverse Polish Notation), I love it!
The first scientiric calculators from HP used RPN.
At the beginning, it seems a bit confusing, and when your are accustomed with it you wouln'd change, at least me.
The first program I wrote when I learned a new language was a RPN Calculator.
You could also have a look at: RPN Calculator and Tape calculator.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Se vuoi il codice sorgente di una calcolatrice te lo do io (è il codice più semplice di questo mondo)
E' più facile che trovare l'errore nel codice di un altro :p
______________________________________________________________________________

If you want the source code of a calculator I'll give it to you (it's the simplest code in this world)
It is easier than finding the error in another's code o_O
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
You may want to have a look at a more complete (and complex) calculator in section Share Your Creations of this Forum.
 
Upvote 0

actarus63

Member
Licensed User
grazie udg è quello che sto facendo e di capire
ringrazio anche stur dus

thanks udg is what I'm doing and understanding
I also thank stur dus
 
Upvote 0
Top