0

I am in need of assistance with my Java program assignment. The assignment is to calculate the distance between two points using java. I completed part one as followed:

import java.util.Scanner;

public class DistanceCalcEasy
{
   public static void main(String[] args)
   {
   // Creating a new scanner object
   System.out.println("Distance Calculator");
   Scanner input = new Scanner(System.in);

   // Getting all of the coordinates
   System.out.print("Enter the X coordinate of the first point: ");
   double x1 = input.nextDouble();
   System.out.print("Enter the Y coordinate of the first point: ");
   double y1 = input.nextDouble();

   System.out.print("Enter the X coordinate of the second point: ");
   double x2 = input.nextDouble();
   System.out.print("Enter the Y coordinate of the second point: ");
   double y2 = input.nextDouble();

   // Calculating the distance between the points
   double distance = Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) );

   // Printing the distance to the User
   System.out.println("The distance between the points is " + distance);
   }
}

Now the problem is I need to do this same program again but the "hard way" by allowing the user to input a coordinate like 1,2 instead of each x and y on their own line. This is what I have started to come up with after a little bit of research:

import java.util.Scanner;

public class DistanceCalcHard
{
   public static void main(String[] args)
   {
      // Creating a new Scanner Object
      System.out.println("Distance Calculator");
      Scanner input = new Scanner(System.in);

      // Getting the data points
      System.out.print("Enter the first point x,y: ");
      String firstPoint = input.nextLine();

      System.out.print("Enter the second point x,y: ");
      String secondPoint = input.nextLine();

      Scanner scan = new Scanner(firstPoint).useDelimiter("\\s*,\\s*");
      while (scan.hasNextDouble() ) 
      { 

      }
      // Calculating the distance

      // Displaying the distance to the user
   }
}

Does that seem like a good start? I was thinking I could make two array's, one for each point, and then do my distance calculation that way. Is there a simpler way to do this or can someone point me in a better direction? Thank You

1
  • int[] point1 = firstPoint.split(",") give you the point in array, [1, 2] for example Commented Jan 15, 2014 at 3:12

2 Answers 2

1

An easier way to go about splitting the string into two values (ie. x,y -> x and y) would be by using the split() operator for a String object.

String[] pointA = firstPoint.split(",");

And the same can be done for the second point. Now you have your two points in arrays where pointA[0] is the x value and pointA[1] is the y value.

More documentation about the method can be found here

Sign up to request clarification or add additional context in comments.

Comments

0

How about something like this:

import java.util.Scanner;

public class DistanceCalcEasy
{
   public static void main(String[] args)
   {
   // Creating a new scanner object
   System.out.println("Distance Calculator");
   Scanner input = new Scanner(System.in);

   // Getting all of the coordinates
   System.out.print("Enter the X,Y coordinate of the first point: ");
   String xy1in = input.nextLine();

   System.out.print("Enter the X,Y coordinate of the second point: ");
   String xy2in = input.nextLine();

   String[] xy1 = xy1in.split(",");
   String[] xy2 = xy2in.split(",");

   double x1 = Double.parseDouble(xy1[0]);
   double y1 = Double.parseDouble(xy1[1]);
   double x2 = Double.parseDouble(xy2[0]);
   double y2 = Double.parseDouble(xy2[1]);

   // Calculating the distance between the points
   double distance = Math.sqrt( Math.pow((x2-x1),2) + Math.pow((y2-y1),2) );

   // Printing the distance to the User
   System.out.println("The distance between the points is " + distance);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.