Richard Nicson
Richard Nicson

Reputation: 147

add value and insert into setText

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

Upvotes: 1

Views: 674

Answers (3)

Zain
Zain

Reputation: 40878

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);
        }

Upvotes: 1

Eng.Mohammad
Eng.Mohammad

Reputation: 37

Well , You have to revise java basics a little bit

        String soma = 1; // you have a problem here call it Problem 1 
        int totalapp = (soma + papp); //  because of Problem 1 you get part of  Problem 2  and because of  papp is a view object, not a value you get the second part  so you have to get it's valued first by getText() Method 



        String totalappString = Integer.toString(totalapp); // and Problem 3 here 
        papp.setText(totalappString);

Problem one is assignment problem String should be between ""

              String soma ="1"; 
or you can consider soma as int from the beginning.It depends on how you are solving the problem, let us just take care of the syntax errors and semantics are upon your logic.

Problem two is conversion problem since papp is a view containing String type the value should be obtained by getText() and then be converted to Integer then used in summation

              int totalapp = (Integer.parseInt(soma) + 
              Integer.parseInt(papp.getText()));//note papp.getText() 

Upvotes: 1

Sandi
Sandi

Reputation: 2731

Java is a statically typed language which means you cannot assign values to incompatible types (String soma = 1; is not a valid assignment). Furthermore papp is an instance of TextView and not an int, so you cannot do int totalapp = (soma + papp);.

You would have to coerce the TextView text to an int before adding it to any other value, for example:

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

int soma = 1;
int pappInt = 0;
try {
    pappInt = Integer.parseInt(papp.getText().toString());
} catch (NumberFormatException nfe) {
    // Cannot parse papp.getText() as an Integer
}

int totalapp = (soma + pappInt);

Upvotes: 2

Related Questions