I want to calculate the distance between two co-ordinates, which are stored in a CVS file. There are two column mentioned in CVS files for X and Y Co-ordinates respectively.
I want to apply Euclidean Distance Formula between those stored points and printing the result on the console. For the same I have retrieved the CVS file's point as an array, printing that array on the console, and applying the Distance Formula, and after that I want to sort them according to ascending order and selecting the one having minimum distance for further problem.
But my problem is that the distance is not being displayed on the console. The code is mentioned below:
import java.util.*;
import java.io.*;
public class distance {
public void euclidianDistanceFromFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine(); // for ignoring the header of file
int row = 0;
int col = 0;
double dist;
String[][] numbers = new String[8][2];
double Cordx[] = new double[8];
double Cordy[] = new double[2];
while ((line = br.readLine()) != null && row < 8) {
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
// get next token and store it in the array
numbers[row][col] = st.nextToken();
col++;
}
col = 0;
row++;
}
for (row = 0; row < 8; row++) {
for (col = 0; col < 2; col++) {
System.out.print(" " + numbers[row][col]);
}
System.out.println(" ");
}
for (row = 0; row < 8; row++) {
for (col = 0; col < 2; col++) {
Cordx[row] = Double.parseDouble(numbers[row][col]);
Cordy[col] = Double.parseDouble(numbers[row][col]);
}
}
for (int i = 0; i < Cordx.length; i++) {
dist = 0;
for (int j = 0; j < Cordy.length; j++) {
double diffx = Cordx[i + 1] - Cordx[i];
double diffy = Cordy[j + 1] - Cordy[j];
dist = dist + Math.sqrt(Math.pow(diffx, 2) + Math.pow(diffy, 2));
}
System.out.println("distance is" + "" + dist);
}
}
public static void main(String[] argv) throws IOException {
try {
distance dist = new distance();
dist.euclidianDistanceFromFile("src\\ploting\\ravm.csv");
// ravm is the cvs file from which i retrieve the points and calculate the distance.
} catch (Exception e) {
e.getMessage();
}
}
}