2

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!

11
  • 2
    Should be somewhere on the parsing I guess -- Doesn't your compiler give you the offending line number? Commented Mar 19, 2014 at 13:06
  • Moreover what do you expect to get trying to parse 105.0,45.0,22.0 as double? Commented Mar 19, 2014 at 13:08
  • If you read all lines into a String[] is there any reason you don't use the appropriate method from the JDK? Further, why copy a perfectly good List to an array only to loop over it? Finally, where do you split on ,? One more thing, what's with calling toString on a String? Commented Mar 19, 2014 at 13:09
  • @devnull it would be the runtime actually Commented Mar 19, 2014 at 13:10
  • You're not splitting the strings up anywhere. You probably need this somewhere: .split(",") Commented Mar 19, 2014 at 13:10

1 Answer 1

1

your inputX = stringArr[i].toString(); is apparently being 0 or something that cannot be parsed to Double. please check that and set some if-condition there if it makes sense for ya..

hope that helps

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

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.