distance between two points

Page 1 of 1

4 Replies - 33245 Views - Last Post: 19 January 2013 - 09:44 PM Rate Topic: -----

#1 Keithabt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-August 08

distance between two points

Post icon  Posted 11 September 2008 - 07:41 PM

I am a beginner and need a little guidence. In my source code I keep getting an error that states" varible x2 might not have been initalized"
does anyone have a suggestion?
import java.util.Scanner;// program uses class Scanner

public class DistanceMethod
{
   // Main method begins execution of Java application
  public static void main( String args[] )
  {
	
		 // create scanner to obtain input from the command line
		  Scanner input = new Scanner( System.in );
		  
		  
		  
		 double x1;
		 double x2;
		 double y1;
		 double y2;
		 double distance;
		 
		  
		  
		 distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); 		  
		  System.out.print( "Enter the the coordinates for x1");// Prompt
		  x1 = input.nextInt(); // Read the number for x1
		  
		  System.out.print( "Enter the the coordinates for x2");// Prompt
		  x2 = input.nextInt(); // Read the number for x2
		  
		  System.out.print( "Enter the the coordinates for y1");// Prompt
		  y1 = input.nextInt(); // Read the number for y1
		  
		  System.out.print( "Enter the the coordinates for x2");// Prompt
		  y2 = input.nextInt(); // Read the number for y2 
		  
		  System.out.print( "The distance between the two points are" +  distance);
		  
		  
  }		  
		  

		  
		  
		  
		  
}


*please use code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: distance between two points

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: distance between two points

Posted 11 September 2008 - 08:01 PM

Your problem is that you have set distance before you have any values for x1, x2, y1, or y2 so it can't come up with anything.

Here, this should work:

import java.util.Scanner;// program uses class Scanner

public class DistanceMethod{
  // Main method begins execution of Java application
  public static void main( String args[] ){
	// create scanner to obtain input from the command line
	Scanner input = new Scanner( System.in );
	double x1;
	double x2;
	double y1;
	double y2;
	double distance;

	System.out.print( "Enter the the coordinates for x1");// Prompt
	x1 = input.nextInt(); // Read the number for x1

	System.out.print( "Enter the the coordinates for x2");// Prompt
	x2 = input.nextInt(); // Read the number for x2

	System.out.print( "Enter the the coordinates for y1");// Prompt
	y1 = input.nextInt(); // Read the number for y1

	System.out.print( "Enter the the coordinates for x2");// Prompt
	y2 = input.nextInt(); // Read the number for y2 

	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); 
	System.out.print( "The distance between the two points are"+distance);
  }
} 



Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 Keithabt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-August 08

Re: distance between two points

Posted 11 September 2008 - 08:26 PM

View PostBetaWar, on 11 Sep, 2008 - 08:01 PM, said:

Your problem is that you have set distance before you have any values for x1, x2, y1, or y2 so it can't come up with anything.

Here, this should work:

import java.util.Scanner;// program uses class Scanner

public class DistanceMethod{
  // Main method begins execution of Java application
  public static void main( String args[] ){
	// create scanner to obtain input from the command line
	Scanner input = new Scanner( System.in );
	double x1;
	double x2;
	double y1;
	double y2;
	double distance;

	System.out.print( "Enter the the coordinates for x1");// Prompt
	x1 = input.nextInt(); // Read the number for x1

	System.out.print( "Enter the the coordinates for x2");// Prompt
	x2 = input.nextInt(); // Read the number for x2

	System.out.print( "Enter the the coordinates for y1");// Prompt
	y1 = input.nextInt(); // Read the number for y1

	System.out.print( "Enter the the coordinates for x2");// Prompt
	y2 = input.nextInt(); // Read the number for y2 

	distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); 
	System.out.print( "The distance between the two points are"+distance);
  }
} 



Hope that helps.



Yes, thank you for explaining it to me!
Was This Post Helpful? 0
  • +
  • -

#4 kestrel  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 5
  • Joined: 19-January 13

Re: distance between two points

Posted 19 January 2013 - 09:43 PM

kEeping things pretty simple here
http://www.youtube.c...h?v=3VjPRtdu1ls
Was This Post Helpful? -2
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: distance between two points

Posted 19 January 2013 - 09:44 PM

Closing yet another useless necro. Please stop spamming your videos.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1