#Bro code broken calculator fix...where did I go wrong?

9 messages · Page 1 of 1 (latest)

hearty schooner
#

I was following this video https://youtu.be/dfhmTyRTCSQ?si=ZL5K8rV76x5NEC2m all the way up to the 32:35 mark and noticed that my calculator is totally wrong (saying 2+2=2048 and disappearing after I type the next number). What have I done wrong for the calculator I'm building based upon the guidance in this tutorial to not work... well ... like a calculator at all? Here's my code and images of the calculator not working: ``````

Java simple calculator program w/ GUI

#Java #calculator #app

(NOT A COMPLETE CALCULATOR)

▶ Play video
modest parcelBOT
#

This post has been reserved for your question.

Hey @hearty schooner! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

limpid talon
#

I compiled it and every time I add an operation I get a NumberFormatException, you sure you posted the latest version of your code?

#

I have the feeling that there is quite some logic missing

#

Also, in actionPerformed(), why do you run the loop 10 times?

#

I see that you need to check all the number buttons, but when it is for example the subButton, you execute it's action 10 times, but the first iteration you reset the textField and then it throws the NumberFormatException

#

Also for the delete function you can use ```java
textField.setText(""); // full clear

// remove last element only

// get last index of the text with zero in mind
int index = (textField.getText().length() != 0) ? textField.getText().length() - 1 : 0;

textField.setText(textField.getText().substring(0, index));

so you do not have to perform the nested loop
#

However from what I saw, none of the logic behind the button event is correct in the terms of being working and achieving the calculation. Are you sure you followed the video exactly?