private void Button_CalculateActionPerformed(java.awt.event.ActionEvent evt) {
try{ a=Double.parseDouble(TextField_First.getText());
b=Double.parseDouble(TextField_Second.getText());
switch(operation){
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
case 5:result=a%b;
break;
case 6:result=Math.pow(a,b);
break;
case 7:
TextField_Second.setText(""+a);
result=Math.sqrt(a);
TextField_Second.setText(""+a);
break;
case 8:result=Math.abs(a);
default:System.out.println("Please select an operation");
}
TextField_Result.setText(""+result);
}catch(NumberFormatException e){Label_error.setText("Please use your
common sense ;-;");}
}
What I'm trying to do is set the value (and text) of TextField_Second
to 0.0, if TextField_Second
is null
/has no value. So if TextField_First
's value is 9, I get the square root, I get an error (NumberFormatException
), but I used try and catch, so if that does happen, a label will appear saying, "Please use your common sense ;-;".
P.S. It only works if TextField_Second
has a value, and it doesn't affect the answer.