# distance between two points

Page 1 of 1

## 4 Replies - 33490 Views - Last Post: 19 January 2013 - 09:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=63482&amp;s=aff550fc9045f705a70965776f7c5a6d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Keithabt

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

# distance between two points

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);

}

}

```

Is This A Good Question/Topic? 0

## Replies To: distance between two points

### #2 BetaWar

• #include "soul.h"

Reputation: 1306
• Posts: 7,678
• 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.

### #3 Keithabt

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

## Re: distance between two points

Posted 11 September 2008 - 08:26 PM

BetaWar, 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!

### #4 kestrel

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

## Re: distance between two points

Posted 19 January 2013 - 09:43 PM

kEeping things pretty simple here

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11447
• Posts: 43,144
• Joined: 27-December 08

## Re: distance between two points

Posted 19 January 2013 - 09:44 PM