Android studio: error: incompatible types: double cannot be converted to String

4.3k views Asked by At

I am new to android studio and trying to make a calculator app. Almost all of my buttons are working (0-9), but I only need to program the addition, subtraction, multiply and divide buttons. Now I got a problem:

error: incompatible types: double cannot be converted to String.

I really don't know how to change this to let it work. Here's a part of my code:

//Button A (Addition) Event Handler
    buttonA.setOnClickListener(
            //Button A Interface
            new Button.OnClickListener(){
                //Button A Callback Method
                public void onClick(View v){
                    TextView output = (TextView)findViewById(R.id.editText);
                    tempDouble = Double.parseDouble(output.getText().toString());
                    output.setText("");
                    sign = "+";
                }
            }
    );

    //Button S (Subtract) Event Handler
    buttonS.setOnClickListener(
            //Button S Interface
            new Button.OnClickListener(){
                //Button S Callback Method
                public void onClick(View v){
                    TextView output = (TextView)findViewById(R.id.editText);
                    tempDouble = Double.parseDouble(output.getText().toString());
                    output.setText("");
                    sign = "-";
                }
            }
    );

    //Button D (Divide) Event Handler
    buttonD.setOnClickListener(
            //Button D Interface
            new Button.OnClickListener(){
                //Button D Callback Method
                public void onClick(View v){
                    TextView output = (TextView)findViewById(R.id.editText);
                    tempDouble = Double.parseDouble(output.getText().toString());
                    output.setText("");
                    sign = "/";
                }
            }
    );

    //Button M (Multiply) Event Handler
    buttonM.setOnClickListener(
            //Button M Interface
            new Button.OnClickListener(){
                //Button M Callback Method
                public void onClick(View v){
                    TextView output = (TextView)findViewById(R.id.editText);
                    tempDouble = Double.parseDouble(output.getText().toString());
                    output.setText("");
                    sign = "*";
                }
            }
    );

    //Button Equals
    buttonE.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    TextView output = (TextView)findViewById(R.id.editText);
                    tempDouble2 = Double.parseDouble(output.getText().toString());

                    if (sign .equals("+")){
                        output.setText(Double.toString(tempDouble + tempDouble2));
                    }
                    else if (sign .equals("-")){
                        output.setText(Double.toString(tempDouble - tempDouble2));
                    }
                    else if (sign .equals("X"))){
                        output.setText(Double.toString(tempDouble * tempDouble2));
                    }
                    else if (sign .equals("/")){
                        if (tempDouble2 == 0){
                            //Cannot devide by zero
                            output.setText("X");
                        }
                        else {
                            output.setText(Double.toString(tempDouble / tempDouble2));
                        }
                    }

                    //Reset the Sign variable
                    sign = "";
                }
            }
    );
}

Also I get the errors:

error: bad operand types for binary operator '-'

error: bad operand types for binary operator '*'

error: bad operand types for binary operator '/'

Could someone with some experience help me please?

1

There are 1 answers

4
xingbin On BEST ANSWER

You can not use '+' operator between two Double objects.

If you want to convert a Double to String, you should use

String.valueOf(tempDouble);

If you want to add two Double objects, you should use

tempDouble.doubleValue() + tempDouble2;