# 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!
}
}

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

What exact error are you getting?

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 08:53 PM. Reason: Wrong program

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

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.

6. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,735
Rep Power
4

## 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 05:58 AM.

7. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

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

Moving to New to Java.

#### Posting Permissions

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