add value and insert into setText

Richard Nicson :

Hello I am creating a game and it has scores I need to make the points are added and changed with setText.

I am having trouble converting because I get the text value in string and summing it is giving error.

    TextView pvoce = (TextView) findViewById(R.id.pvoce);
        TextView papp = (TextView) findViewById(R.id.papp);

if (escolhaApp == "pedra" && escolhaUsuario == "tesoura" || escolhaApp == "papel" && escolhaUsuario == "pedra" || escolhaApp == "tesoura" && escolhaUsuario == "papel") {
            textoResultado.setText("    You Lose   ");
            String soma = 1;
            int totalapp = (soma + papp);
            String totalappString = Integer.toString(totalapp);
            papp.setText(totalappString);

        } else if (escolhaUsuario == "pedra" && escolhaApp == "tesoura" || escolhaUsuario == "papel" && escolhaApp == "pedra" || escolhaUsuario == "tesoura" && escolhaApp == "papel") {
            textoResultado.setText("   You Win   ");
            int soma = 1;
            int totalvoce = soma + pvoce;
            String totalvoceString = Integer.toString(totalvoce);
            pvoce.setText(totalvoceString);
        }

error: incompatible types: int cannot be converted to String error: incompatible types: String cannot be converted to int error: bad operand types for binary operator '+' first type: int second type: TextView

Zain :

I won't have much stress on errors as it's already covered by previous answers. But here you may find the correct solution by commenting out your line of codes that have problems with some explanation of the issues, and writing directly below of each line the corrected code

TextView pvoce = (TextView) findViewById(R.id.pvoce);
TextView papp = (TextView) findViewById(R.id.papp);

if (escolhaApp == "pedra" && escolhaUsuario == "tesoura" || escolhaApp == "papel" && escolhaUsuario == "pedra" || escolhaApp == "tesoura" && escolhaUsuario == "papel") {
            textoResultado.setText("    You Lose   ");
            // String soma = 1; // wrong as java is statically typed language, you can't assign a int into a String 
            int soma = 1;

           //  int totalapp = (soma + papp); // papp is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
            int totalapp = soma + Integer.parseInt(papp.getText().toString());

            String totalappString = Integer.toString(totalapp);
            papp.setText(totalappString);

        } else if (escolhaUsuario == "pedra" && escolhaApp == "tesoura" || escolhaUsuario == "papel" && escolhaApp == "pedra" || escolhaUsuario == "tesoura" && escolhaApp == "papel") {
            textoResultado.setText("   You Win   ");
            int soma = 1;
           //  int totalvoce = soma + pvoce; // pvoce is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
            int totalvoce = soma + Integer.parseInt(pvoce.getText().toString());

            String totalvoceString = Integer.toString(totalvoce);
            pvoce.setText(totalvoceString);
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=309065&siteId=1