# Thread: How to you write a java program using the distance formula?

1. Member Join Date
Sep 2013
Posts
4
Rep Power
0

## How to you write a java program using the distance formula?

import java.util.Scanner;

public class Distance
{

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

System.out.print("Enter first two points (x1, y1) :");
double x1 = input.nextDouble();
double y1 = input.nextDouble();

System.out.print("Enter second two points (x2, y2) :");
double x2 = input.nextDouble();
double y2 = input.nextDouble();

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

System.out.print("The distance of the two points is " + distance); //I'm using JGRASP AND my error is here!
}
}  Reply With Quote

2. ## Re: How to you write a java program using the distance formula?

What exact error are you getting?  Reply With Quote

3. Member Join Date
Sep 2013
Posts
4
Rep Power
0

## Re: How to you write a java program using the distance formula?

import java.util.Random;
public class Distance {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter first two points (x1, y1) :");
double firstPoints = input.nextDouble();

System.out.print("Enter second two points (x2, y2) :");
double secondPoints = input.nextDouble();

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

System.out.print("The distance of the two points is " + distance);
}
}
Last edited by fscj; 09-26-2013 at 07:53 PM. Reason: Wrong program  Reply With Quote

4. Member Join Date
Sep 2013
Posts
4
Rep Power
0

## Re: How to you write a java program using the distance formula?

Distance.java:14: error: ')' expected
double distance = Math.pow((x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1), 0.5;
^
1 error  Reply With Quote

5. ## Re: How to you write a java program using the distance formula?

Count your opening and closing parenthesis. You should have the same number of each.  Reply With Quote

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

## Re: How to you write a java program using the distance formula?

It has to do with the fact hat you can't multiply in Java like (x2-x1)(x2-x1). It has to be (x2-x1)*(x2-x1).
And BTW, there is a square root method in the Math class.

Regards,
Jim
Last edited by jim829; 09-27-2013 at 04:58 AM.  Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: How to you write a java program using the distance formula?

Moving to New to Java.  Reply With Quote

#### Posting Permissions

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