# Can someone help me with this code?

• 10-23-2009, 04:46 PM
Alicia
Can someone help me with this code?
I'm making a program to find the distance between two points.

Code:

```import cs1.Keyboard; import java.text.NumberFormat; import static java.lang.Math.*; public class Distance {         public static void main (String[] args)     {                 System.out.println ("Enter the first x coordinate: ");         double x1 = Keyboard.readInt();                 System.out.println ("Enter the first y coordinate: ");         double y1 = Keyboard.readInt();                 System.out.println ("Enter the second x coordinate: ");         double x2 = Keyboard.readInt();                 System.out.println ("Enter the second y coordinate: ");         double y2 = Keyboard.readInt();                 double distance = sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));         System.out.println ("The distance is:" + distance);     } }```
I keep getting the error message:
unexpected type. At the sqrt line.
And I use BlueJ
• 10-23-2009, 04:48 PM
Fubarable
There is no method in your class called sqrt. You probably want to use the static sqrt method found in the Math class. To use this, you must call it on the class:

not
sqrt(...)

but
Math.sqrt(...)
• 10-23-2009, 04:57 PM
JosAH
... also Java is too stupid to smell that it has to multiply two operands when they are next to each other.

kind regards,

Jos
• 10-23-2009, 04:57 PM
Fubarable
Quote:

Originally Posted by JosAH
... also Java is too stupid to smell that it has to multiply two operands when they are next to each other.

and it appears so am I for not noticing this!
• 10-23-2009, 05:09 PM
JosAH
Quote:

Originally Posted by Fubarable
and it appears so am I for not noticing this!

Inheriting from C's syntax is a mixed blessing, i.e. in C the expression (x-y)(x-y) would've been perfectly valid but for other reasons one would expect.

kind regards,

Jos
• 10-23-2009, 05:11 PM
Fubarable
Quote:

Originally Posted by JosAH
Inheriting from C's syntax is a mixed blessing, i.e. in C the expression (x-y)(x-y) would've been perfectly valid but for other reasons one would expect.

I'm guessing that this doesn't multiply in C, but what does it do?
• 10-23-2009, 05:14 PM
JosAH
Quote:

Originally Posted by Fubarable
I'm guessing that this doesn't multiply in C, but what does it do?

I made a mistake, you need C++ for that and some sneaky operator overloading but indeed, it won't multiply but call some function somewhere, somehow, someway ... ;-)

kind regards,

Jos
• 10-24-2009, 12:37 AM
Alicia
Ive already used Math.sqrt and I still get the same error.. I need to finish this program for my class.
And also I cant use c++, its a java class.
• 10-24-2009, 12:40 AM
r035198x
Did you include the * to tell Java that you are multiplying like has already been suggested?
• 10-24-2009, 12:53 AM
Alicia
Omg, r035198x, THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
• 10-25-2009, 01:22 PM
JosAH
Quote:

Originally Posted by Alicia
Omg, r035198x, THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

kind regards,

Jos
• 10-27-2009, 10:57 AM
freakydeveloper