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  Reply With Quote

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.  Reply With Quote

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.  Reply With Quote

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.  Reply With Quote

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?  Reply With Quote

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.  Reply With Quote

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```  Reply With Quote

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

## Re: Distance Formula Problem

Nevermind I fixed the error. Thanks!  Reply With Quote

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  Reply With Quote

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  Reply With Quote

11. ## Re: Distance Formula Problem Originally Posted by jim829 Just use Math.sqrt().

kind regards,

Jos  Reply With Quote

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   Reply With Quote