Results 1 to 13 of 13
 10232009, 04:46 PM #1Member
 Join Date
 Oct 2009
 Posts
 4
 Rep Power
 0
Can someone help me with this code?
I'm making a program to find the distance between two points.
Java 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((x1x2)(x1x2) + (y1y2)(y1y2)); System.out.println ("The distance is:" + distance); } }
unexpected type. At the sqrt line.
And I use BlueJ
Please helppp! :)

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(...)
 10232009, 04:57 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21
... also Java is too stupid to smell that it has to multiply two operands when they are next to each other.
kind regards,
Jos

 10232009, 05:09 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21

 10232009, 05:14 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21
 10242009, 12:37 AM #8Member
 Join Date
 Oct 2009
 Posts
 4
 Rep Power
 0
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.
 10242009, 12:40 AM #9Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 8
Did you include the * to tell Java that you are multiplying like has already been suggested?
 10242009, 12:53 AM #10Member
 Join Date
 Oct 2009
 Posts
 4
 Rep Power
 0
Omg, r035198x, THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 10252009, 01:22 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,680
 Blog Entries
 7
 Rep Power
 21
 10272009, 10:57 AM #12Member
 Join Date
 Oct 2009
 Posts
 3
 Rep Power
 0
Instead of this
double distance = sqrt((x1x2)(x1x2) + (y1y2)(y1y2));
Use the static method's provided by the Math class like Math.sqrt(), Math.pow()
final int SQUARE = 2;
double distance = Math.sqrt(Math.pow((x2x1), SQUARE)+ Math.pow((y2y1), SQUARE));

And the advantage of using Math.pow is (besides being significantly slower than the previous solutions, that is)?
Similar Threads

Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08122009, 12:14 PM 
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08092009, 02:21 PM 
Why doesn't this code accept my code?
By PeterFeng in forum New To JavaReplies: 5Last Post: 02032009, 02:39 PM 
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03192008, 11:12 PM 
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04262007, 04:52 PM
Bookmarks