Java Q: How do I do the square-root of a string?

Akerman

Member
Nov 8, 2001
67
0
0
I'm making a simple calculator, programmed in a java-applet (not javascript, regular java).
I want to make a button that does the squareroot of whatever is in the display and returns it immediately, or
maybe does the cosine of a value in the display and returns it likewise.

When I tro do this it says that I need a double in order to do this, and my display is a
TextField, where I retrieve the value in it by calling displayInOut.getText() - how do I do the square-root
for instance (+Math.sqrt) of what ever is in the display when the user presses the sqrt-button?

hope to hear from you...
 

cool

Senior member
Jun 17, 2000
413
0
0
Math.sqrt(Double.valueOf(displayInOut.getText()));

OR

Math.sqrt(Double.parseDouble(displayInOut.getText()));
 

Akerman

Member
Nov 8, 2001
67
0
0
2nd option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: cannot resolve symbol
symbol : method setText
(double)location: class java.awt.TextField
displayInOut.setText(Math.sqrt(Double.parseDouble(displayInOut.getText()))));
^

1st option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: sqrt(double) in java.lang.Math cannot be applied to (java.lang.Double)
displayInOut.setText(Math.sqrt(Double.valueOf(displayInOut.getText())));
^

What's still wrong!?`=/
 

cool

Senior member
Jun 17, 2000
413
0
0


<< 2nd option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: cannot resolve symbol
symbol : method setText
(double)location: class java.awt.TextField
displayInOut.setText(Math.sqrt(Double.parseDouble(displayInOut.getText())))); <---- one brace too much!!!
^

1st option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: sqrt(double) in java.lang.Math cannot be applied to (java.lang.Double)
displayInOut.setText(Math.sqrt(Double.valueOf(displayInOut.getText()))); <---- casting problem?
^
>>



You're setting and getting at the same time?! displayInOut.setText(...displayInOut.getText()) --> how should this work? You make input to the TextField and at the same time you are reading output from it. Maybe I'm wrong... if someone could confirm this, please!

 

Akerman

Member
Nov 8, 2001
67
0
0
Hmm! it works with the other functions on the calc, like multiply, add, and divide:
if (e.getSource()== but[14])
displayInOut.setText(displayInOut.getText()+"*");
 

bevancoleman

Golden Member
Jun 24, 2001
1,080
0
0


<< 2nd option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: cannot resolve symbol
symbol : method setText
(double)location: class java.awt.TextField
displayInOut.setText(Math.sqrt(Double.parseDouble(displayInOut.getText()))));
^

1st option gives me this error msg:
C:\Program Files\JCreator Pro\MyProjects\Lom1\Lom1.java:144: sqrt(double) in java.lang.Math cannot be applied to (java.lang.Double)
displayInOut.setText(Math.sqrt(Double.valueOf(displayInOut.getText())));
^

What's still wrong!?`=/
>>



The 1st option seems to be the correct one, but it looks like the sqrt method disn't accept Doubles as a param, check the doco to confirm this and see what it will take

cool
what he is doing is fully valid because he isn't setting and getting at the same time. Remember that with brakets, the inner most set get evaluated first.

Akerman


<< Hmm! it works with the other functions on the calc, like multiply, add, and divide:
if (e.getSource()== but[14])
displayInOut.setText(displayInOut.getText()+"*");
>>


And back to akerman. That will only add a '*' to the end of the string, it wont multipy anything. The solution cool provided with do a sqrt.
 

cool

Senior member
Jun 17, 2000
413
0
0


<<
cool
what he is doing is fully valid because he isn't setting and getting at the same time. Remember that with brakets, the inner most set get evaluated first.
>>



Wow, I didn't know that! Well, I never checked it out, I always added a variable to hold the data. Yeah, I know that's poor... ;)
Thanks, bevancoleman! Sorry Akerman for confusion...
 

CodeJockey

Member
May 1, 2001
177
0
0
I don't know Java, but I will hazard a guess that there is no setText() method that takes a double ( the type returned by the Math.sqrt() method) as an argument.

That is what the error method seems to be saying...if you adjust the newline position it becomes more clear.



<<
cannot resolve symbol
symbol : method setText
(double)location: class java.awt.TextField
>>



becomes



<<
cannot resolve symbol
symbol : method setText(double)
location: class java.awt.TextField
>>



You probably need to wrap the Math.sqrt(...) call in another method to convert it to Text.
 

m0ti

Senior member
Jul 6, 2001
975
0
0
the last answer's correct. In Java you need to provide an explicit cast.

So you could do

displayInOut.setText((String)Math.sqrt(Double.parseDouble(displayInOut.getText()))));

which should work, though it's less correct than:

displayInOut.setText(Math.sqrt(Double.parseDouble(displayInOut.getText()))).toString());

which uses the commonly known toString function which exists in nearly all printable objects, or you can use the following, which implicitly utilizes the toString() method.

displayInOut.setText("" + Math.sqrt(Double.parseDouble(displayInOut.getText()))));


Hope that answers your question.