Results 1 to 12 of 12
Thread: Distance Formula Problem
 08312015, 01:33 AM #1Member
 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);
System.out.print("Please enter x1, y1: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Please enter x2, y2: ");
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
 08312015, 02:13 AM #2Senior 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.
 08312015, 03:08 AM #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...
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; 08312015 at 03:11 AM.
If you don't understand my response, don't ignore it, ask a question.
 08312015, 03:35 AM #4Member
 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); System.out.print("Please enter x1, y1: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); System.out.print("Please enter x2, y2: "); 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]
Distance.java:24: error: ';' expected
distance = Math.pow((x2  x1) * (x2  x1) + (y2  y1) * (y2  y1)), 0.5);[/CODE]Last edited by MrLegitPancakes; 08312015 at 04:05 AM.
 08312015, 03:41 AM #5Member
 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?
 08312015, 03:56 AM #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.If you don't understand my response, don't ignore it, ask a question.
 08312015, 04:04 AM #7Member
 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
 08312015, 04:07 AM #8Member
 Join Date
 Aug 2015
 Posts
 18
 Rep Power
 0
Re: Distance Formula Problem
Nevermind I fixed the error. Thanks!
 08312015, 08:26 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 08312015, 02:25 PM #10Senior 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 08312015, 02:45 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 08312015, 03:43 PM #12Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Distance Formula Problem
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

JDBC, Beans, And Great Distance Formula
By HeavyTech in forum JDBCReplies: 6Last Post: 03182015, 05:06 PM 
How to you write a java program using the distance formula?
By fscj in forum New To JavaReplies: 6Last Post: 09272013, 09:33 AM 
How to you write a java program using the distance formula?
By fscj in forum Math ExamplesReplies: 6Last Post: 09272013, 09:33 AM 
Distance Formula and Random numbers code
By granslime in forum New To JavaReplies: 10Last Post: 09262013, 07:27 PM 
Distance formula
By cost63 in forum New To JavaReplies: 2Last Post: 07092013, 03:35 AM
Bookmarks