java - Sqrt function invokes an error -
i'm trying aplication calculates equation. need use sqrt errors after trying different metods i've seen on internet. code:
public void calculate(view v){ edittext number1text=(edittext)findviewbyid(r.id.num1text); edittext number2text=(edittext)findviewbyid(r.id.num2text); edittext number3text=(edittext)findviewbyid(r.id.num3text); int num1=integer.parseint(number1text.gettext().tostring()); int num2=integer.parseint(number2text.gettext().tostring()); int num3=integer.parseint(number3text.gettext().tostring()); integer del= num2*num2+4*num1*num3 ; integer first=-num2-math.sqrt(del)/2*num1 ; -getting errors here integer second=-num2+math.sqrt(del)/2*num1 ; -and here textview delta=(textview)findviewbyid(r.id.deltatxt); textview x1=(textview)findviewbyid(r.id.x1txt); textview x2=(textview)findviewbyid(r.id.x2txt); delta.settext("delta:"+del.tostring()); x1.settext("x1:"+first.tostring()); x2.settext("x2:"+second.tostring());
i have imported java.math.mathcontext
, import java.lang.math
you getting type mismatch on these lines understand it:
integer first=-num2-math.sqrt(del)/2*num1 ; integer second=-num2+math.sqrt(del)/2*num1 ;
the problem because math.sqrt(double a)
returns double
, trying set result integer
.
why assign square root integer anyways?
you can change them doubles, so:
double first=-num2-math.sqrt(del)/2*num1 ; double second=-num2+math.sqrt(del)/2*num1 ;
also, on these lines:
delta.settext("delta:"+del.tostring()); x1.settext("x1:"+first.tostring()); x2.settext("x2:"+second.tostring());
you won't allowed use .tostring()
on double type. avoid that, remove it. having strings in front of double
value display without errors.
delta.settext("delta:" + del); x1.settext("x1:" + first); x2.settext("x2:" + second);
Comments
Post a Comment