Chris Chris - 3 years ago 229
Java Question

Math.Sqrt(x); not working?

I'm making a calculator and when trying to make the square root function, it outputs the number you put in, not the square root. Here's the bit of code that applies to the square root function.

SquareRoot = (Button)findViewById(;
SquareRoot.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
x = TextBox.getText();
xconv = Double.parseDouble(x.toString());
answer = Double.toString(xconv);


Just to give some information on this, x is a CharSequence, xconv is x converted to double, and answer is a string value. Thanks.

Answer Source

That is because Math.sqrt returns the sqrt, it does not modify the passed in value.

xconv = Math.sqrt(xconv);

is what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download