-1
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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You need to first check TextField_Second. is null – MinA May 10 '18 at 05:07
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Don't mix Swing & AWT components without good reason. In AWT we might use a `TextField`, but in Swing would use `JTextField`. – Andrew Thompson May 10 '18 at 08:04
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson May 11 '18 at 14:13

1 Answers1

-1

Try something like this;

Note: you have not add break statement to case 8:

try
{
    if (!TextField_First.getText().equals(""))
    {
        if(TextField_Second.getText().equals(""))
        {
            TextField_Second.setText("0.0");
        }
        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);
                break;
            default:System.out.println("Please select an operation");
                break;
    }
}
MinA
  • 405
  • 4
  • 9
  • Because of me being impatient, I tried doing it while waiting for an answer and it worked! Thanks anyways :D try{ success = false; if((operation == 7)||(operation == 8)||(operation == 9)||(operation == 10)){ a=Double.parseDouble(TextField_First.getText()); }else if((operation==1)||(operation == 2)||(operation == 3)||(operation==4)||(operation == 5)){ a=Double.parseDouble(TextField_First.getText()); b=Double.parseDouble(TextField_Second.getText()); }... – CottonTheButton May 10 '18 at 06:19
  • @icebunny08 I'm not think your way is good, since it has only few operations you can follow that but if it is 20 or more operations it looks ugly. you can first check the texfield values and then do what ever operation. and according to your code if it is operation 2 or 3 or4 or 5 and you have not have values for TextField_Second or TextField_First you will get the numberformat exception – MinA May 10 '18 at 06:28
  • I know. I got pissed off because It gave me an error after adding the 11th operator. Any advice? – CottonTheButton May 10 '18 at 12:15
  • what did you mean by 11th operator? – MinA May 10 '18 at 15:11
  • I don't mean 11th operator, as in it's an operator, what I'm trying to say is that if the integer operation is >= 11, it stops working. – CottonTheButton May 10 '18 at 21:33
  • you can provide some instructions like "enter 1 to do addition:" , "enter 2 to do subtraction" like wise. – MinA May 11 '18 at 03:09