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

Printable View

• 09-26-2013, 07:20 PM
fscj
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!
}
}
• 09-26-2013, 07:39 PM
KevinWorkman
Re: How to you write a java program using the distance formula?
What exact error are you getting?
• 09-26-2013, 07:48 PM
fscj
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);
}
}
• 09-26-2013, 07:51 PM
fscj
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
• 09-26-2013, 08:13 PM
KevinWorkman
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.
• 09-26-2013, 08:40 PM
jim829
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
• 09-27-2013, 09:33 AM
Tolls
Re: How to you write a java program using the distance formula?
Moving to New to Java.