Results 1 to 13 of 13
  1. #1
    Alicia is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default 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((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
    Please helppp! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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(...)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

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

    kind regards,

    Jos

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JosAH View Post
    ... 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!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by JosAH View Post
    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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    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

  8. #8
    Alicia is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you include the * to tell Java that you are multiplying like has already been suggested?

  10. #10
    Alicia is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Omg, r035198x, THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Alicia View Post
    Omg, r035198x, THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    People don't read anymore: I already mentioned this in reply #3; if you want help you should read the replies.

    kind regards,

    Jos

  12. #12
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Instead of this

    double distance = sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));

    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((x2-x1), SQUARE)+ Math.pow((y2-y1), SQUARE));

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •