Try this :
float result = (Integer.parseInt(editText1.getText().toString()) +Integer.parseInt(editText2.getText().toString())+Integer.parseInt(editText3.getText().toString()))/ 3;
textView1.setText(""+result );
Pregunta
Why I'm getting error on this line ?
textView1.setText((editText1.getText() + editText2.getText() + (editText3.getText)) / 3);
Can we do math operations in setText
method ?
Solución 2
Try this :
float result = (Integer.parseInt(editText1.getText().toString()) +Integer.parseInt(editText2.getText().toString())+Integer.parseInt(editText3.getText().toString()))/ 3;
textView1.setText(""+result );
Otros consejos
Replace
textView1.setText((editText1.getText() + editText2.getText() + editText3.getText) / 3);
with
textView1.setText(""+(Integer.parseInt(editText1.getText().toString()) + Integer.parseInt(editText2.getText().toString()) + Integer.parseInt(editText3.getText().toString())) / 3);
editText1.getText()
is returning a string. Adding the other getText's is performing string concatenation.
You need to convert the values to integer (or float) and then divide:
textView1.setText(String.valueOf((Integer.valueOf(editText1.getText()) + Integer.valueOf(editText2.getText()) + Integer.valueOf(editText3.getText)) / 3));
editText1.getText()
returns Editable
which implements CharSequence
. So you cannot directly perform arithmetic operations on such data types until you convert them into correct ones. For example: int
, double
...
These methods will return String
editText1.getText()
editText2.getText()
editText3.getText()
You need to cast to Integer or double.
eg Integer.pargeInt(editText1.getText())
for illustration try this one
String s="2";
System.out.println(s/3);