Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Distance Formula Problem

    Hello everyone,

    I am not very experienced with Java. I just started programming, and I run into all sorts of problems. Most of the time I can fix them myself, but I have stumbled across one that I can't. I am trying to write a program that can calculate the distance between two points using the distance formula, but I keep getting an error... I don't know if the is a way I can use a square root, but I am trying to use a power, and it is not working. I tried inserting a semi colon like it says to, but it messes everything up. If you could help me out, that would be great. Thanks!


    import java.util.Scanner;

    public class Distance
    {
    public static void main(String [] args)
    {
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter x1, y1: ");
    double x1 = input.nextDouble();
    double y1 = input.nextDouble();

    System.out.print("Please enter x2, y2: ");
    double x2 = input.nextDouble();
    double y2 = input.nextDOuble();

    Math.pow((x2 - x1) + (y2 - y1)), 0.5);

    System.out.print("The distance between the two points is ");
    }
    }

    Error:
    Distance.java:23: error: ';' expected
    Math.pow((x2 - x1) + (y2 - y1)), 0.5);
    ^
    1 error

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Distance Formula Problem

    1. You have to assign the result to a variable and then print out the variable after your sysout statement
    2. If my vague memory of Phytagoras is right, your formula is wrong.
    3. working with pasted- in text is very difficult and you might want to put it into [code] formats.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Distance Formula Problem

    Please edit your post and wrap the code in code tags to preserve its formatting and make it easier to read.

    I keep getting an error...
    Check that the ()s are paired.

    What is supposed to happen to the value returned by the pow() method? The code is ignoring it. It should be assigned to a variable or printed.
    Last edited by Norm; 08-31-2015 at 03:11 AM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Distance Formula Problem

    Ok this is my new set of code:
    import java.util.Scanner;

    Java Code:
    public class Distance 
    {
       public static void main(String [] args)
       {
          Scanner input = new Scanner(System.in);
          
          System.out.print("Please enter x1, y1: ");
          double x1 = input.nextDouble();
          double y1 = input.nextDouble();
          
          System.out.print("Please enter x2, y2: ");
          double x2 = input.nextDouble();
          double y2 = input.nextDOuble();
          
          double distance;
          distance = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)), 0.5);
          
          System.out.print("The distance between the two points is " + distance);
       }
    }
    [CODE]
    Same error:
    Distance.java:24: error: ';' expected
    distance = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)), 0.5);[/CODE]
    Last edited by MrLegitPancakes; 08-31-2015 at 04:05 AM.

  5. #5
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Distance Formula Problem

    I think that the () are paired now, and did I assign the variable right?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Distance Formula Problem

    Please edit your post and wrap the code in code tags to preserve its formatting and make it easier to read.

    Check that ALL the ()s are paired. Count the (s and then count the )s and see if there are the same number of each.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Distance Formula Problem

    And thanks, I fixed the () problem, but I got a new error:


    Java Code:
    Distance.java:21: error: cannot find symbol
          double y2 = input.nextDOuble();
                           ^
      symbol:   method nextDOuble()
      location: variable input of type Scanner
    1 error

  8. #8
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Distance Formula Problem

    Nevermind I fixed the error. Thanks!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Distance Formula Problem

    The Math class has a (static) method hypot(double x, double y) that can do it all for you; no need to juggle with pow( ..., 0.5) ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Distance Formula Problem

    Just use Math.sqrt().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Distance Formula Problem

    Quote Originally Posted by jim829 View Post
    Just use Math.sqrt().
    No, read my reply #9.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Distance Formula Problem

    Quote Originally Posted by JosAH View Post
    No, read my reply #9.
    I did read your reply. I considered telling the OP about the Math.hypot() before I read your post. But I then thought this may have been an exercise on computing the distance formula. He can always use the class method to check his results.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. JDBC, Beans, And Great Distance Formula
    By HeavyTech in forum JDBC
    Replies: 6
    Last Post: 03-18-2015, 05:06 PM
  2. Replies: 6
    Last Post: 09-27-2013, 09:33 AM
  3. Replies: 6
    Last Post: 09-27-2013, 09:33 AM
  4. Distance Formula and Random numbers code
    By granslime in forum New To Java
    Replies: 10
    Last Post: 09-26-2013, 07:27 PM
  5. Distance formula
    By cost63 in forum New To Java
    Replies: 2
    Last Post: 07-09-2013, 03:35 AM

Posting Permissions

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