1. Member
Join Date
Aug 2015
Posts
18
Rep Power
0

## 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);

double x1 = input.nextDouble();
double y1 = input.nextDouble();

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. Senior Member
Join Date
Sep 2014
Location
MA, USA
Posts
399
Rep Power
6

## 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. ## 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.

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

## 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);

double x1 = input.nextDouble();
double y1 = input.nextDouble();

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. Member
Join Date
Aug 2015
Posts
18
Rep Power
0

## Re: Distance Formula Problem

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

6. ## 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.

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

## 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. Member
Join Date
Aug 2015
Posts
18
Rep Power
0

## Re: Distance Formula Problem

Nevermind I fixed the error. Thanks!

9. ## 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

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

## Re: Distance Formula Problem

Just use Math.sqrt().

Regards,
Jim

11. ## Re: Distance Formula Problem

Originally Posted by jim829
Just use Math.sqrt().

kind regards,

Jos

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

## Re: Distance Formula Problem

Originally Posted by JosAH
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

#### Posting Permissions

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