We help IT Professionals succeed at work.

sqrt(double) in java.lang.Math cannot be applied to (java.lang.String)

modsiw
modsiw asked
on
1,611 Views
Last Modified: 2008-01-09
   [javac] C:\FWSrc\src\com\floorwizard\applet\estimation\EW_Util_Spc.java:1089: sqrt(double) in java.lang.Math cannot be applied to (java.lang.String)
    [javac]         adjustLength = (int)Math.floor(Math.sqrt(Math.pow(adjustDisplacement.x,2.0) + Math.pow(adjustDisplacement.y,2.0)));
    [javac]                                            ^

The carrot is under the dot in Math.sqrt .


adjustDisplacement is a ...point2D.Double()
adjustLength is an int


I don't get it...
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Do a Double.parseDouble()

boefore doing a sqrt
CERTIFIED EXPERT

Commented:
adjustLength = (int)Math.floor(Math.sqrt(Double.parseDouble(Math.pow(adjustDisplacement.x,2.0) + Math.pow(adjustDisplacement.y,2.0))));

Author

Commented:
java.lang.Math.pow(....) returns a double; not a string.
but it takes double NOT string
CERTIFIED EXPERT

Commented:
i didnt notice that
but  sqrt(double) in java.lang.Math cannot be applied to (java.lang.String) means you are trying to do a sqrt on a string instead of double
CERTIFIED EXPERT

Commented:
   public static void main(String[] args)
    {
        int adjustLength = (int) Math.floor(Math.sqrt(Math.pow(2,2.0) + Math.pow(2,2.0)));
        System.out.println(adjustLength);
    }
works fine

dont know why you get that...

try pasting the above in your code in ur ide and see

what ide are you using

Author

Commented:
From docs; in java.lang.Math:    Neither method is overloaded.

static double       pow(double a, double b)
static double       sqrt(double a)
static double       floor(double a)


>>but  sqrt(double) in java.lang.Math cannot be applied to (java.lang.String) means you are trying to do a sqrt on a string instead of double
Indeed it does, but how did it get the String?

My best guess is that it thinks the + is the concat operator and not add



As far as I can tell, there is no String in that line of code. Hence, why I don't get it.
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That works.

This also works
 public static void main(String[] args)
    {
        int adjustLength = (int) Math.floor(Math.sqrt(Math.pow(2.0,2.0) + Math.pow(2.0,2.0)));
        System.out.println(adjustLength);
    }

Author

Commented:
To be exact: I did it in the start() of an applet and not main. That shouldn't make a difference though.

Author

Commented:
Figured it out, it was an incorrect import. Two thumbs up for compiler error reporting. =)


import java.awt.geom.Point2D.Double; // This is an incorrect import of an inner class.

import java.awt.geom.Point2D; // This is correct.


Many thanks Contactkarthi.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.