I just started programming and I've stumbled on some problems.
I made a simple calculator in Netbeans IDE.
The first time i run the calculator and i for example enter: 100/5=20 Then i click my "clear button" and the when i enter: 100/5= 0
However the weird part is that sometimes when i re-run the calculator it works after i use my clear button. Sometimes it does not.
I'm guessing there are some bugs happening? Would appreciate all the help I can get!
private void numberButtonClicked(String numberClicked) {
String input = txtInputfield.getText();
if(txtInputfield.getText().isEmpty()&&operator==null)
txtInputfield.setText(numberClicked);
else if (operator!=null)
{
.
txtInputfield.setText(txtInputfield.getText() + numberClicked);
value2 = Integer.parseInt(value2 + numberClicked);
}
else
txtInputfield.setText(txtInputfield.getText()+ numberClicked);
}
private void btn1MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("1");
}
private void btn2MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("2");
}
private void btn3MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("3");
}
private void btn4MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("4");
}
private void btn5MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("5");
}
private void btn6MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("6");
}
private void btn7MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("7");
}
private void btn8MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("8");
}
private void btn9MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("9");
}
private void btn0MouseClicked(java.awt.event.MouseEvent evt) {
numberButtonClicked("0");
}
private void btnClearMouseClicked(java.awt.event.MouseEvent evt) {
value2 = 0;
txtInputfield.setText("");
value1 = 0;
txtInputfield.setText("");
}
private void operatorClicked(String operatorName, String operatorSymbol){
if(txtInputfield.getText().isEmpty())
return;
.
if (operator!=null&&value2!=0) {
double answer = 0;
if (operator=="plus")
answer = value1 + value2;
else if (operator=="minus")
answer = value1 - value2;
else if (operator=="multiplication")
answer = value1 * value2;
else if (operator=="division")
answer = value1/value2;
value1=(int)answer;
value2 = 0;
txtInputfield.setText("");
operator = operatorName;
} else {
value1 = Integer.parseInt(txtInputfield.getText());
txtInputfield.setText(txtInputfield.getText() + " " + operatorSymbol + " ");
operator = operatorName;
}
}
private void btnPlusMouseClicked(java.awt.event.MouseEvent evt) {
operatorClicked("plus","+");
}
private void btnMinusMouseClicked(java.awt.event.MouseEvent evt) {
operatorClicked("minus","-");
}
private void btnDivisionMouseClicked(java.awt.event.MouseEvent evt) {
operatorClicked("division", "/");
}
private void btnMultiplicationMouseClicked(java.awt.event.MouseEvent evt) {
operatorClicked("multiplication", "*");
}
private void btnEqualMouseClicked(java.awt.event.MouseEvent evt) {
double answer = 0;
if (operator=="plus")
answer = value1 + value2;
else if (operator=="minus")
answer = value1 - value2;
else if (operator=="multiplication")
answer = value1 * value2;
else if (operator=="division")
answer = value1 / value2;
String input = Double.toString(answer);
txtInputfield.setText(input);
}