To run an algorithm I need to use as input a text file. Input Example:
105.0,45.0,22.0
240.0,3.0,50.0
4344.0,4.0,55.0
On the algorithm i need to compute each row with each other(1st row with itself,2nd and 3rd row. 2nd row with 1st row, itself and 3rd row. 3rd row with 1st row,2nd row and itself). Output is a distance matrix with double values (input*input matrix so a 3*3 matrix in this case)
So far my code looks like this:
public class Distance{
public static double calcDist(int dim, double[] x, double[] y) {
//do computation here
//output printed here
}
public static void main(String[] args) throws IOException {
String str;
String inputX;
String inputY;
double arrayX = 0;
double arrayY = 0;
BufferedReader in = new BufferedReader(new FileReader("C:/input.txt"));
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}
for(int i=0; i < list.size(); i++){
for(int j=0; j < list.size(); j++){
inputX = list.get(i);
arrayX = Double.parseDouble(inputX);
inputY = list.get(j);
arrayY = Double.parseDouble(inputY);
double[] x = {arrayX};
double[] y = {arrayY};
calcDist(x.length,x,y);
}
}
The distance method runs on its own fine, but i'm getting various errors on the main. Numberformatexceptions. Should be somewhere on the parsing I guess. Help appreciated. Thanks in advance!
Should be somewhere on the parsing I guess-- Doesn't your compiler give you the offending line number?105.0,45.0,22.0as double?String[]is there any reason you don't use the appropriate method from the JDK? Further, why copy a perfectly goodListto an array only to loop over it? Finally, where do you split on,? One more thing, what's with callingtoStringon aString?.split(",")