简体   繁体   English

将文本解析为双数组

[英]Parsing Text to double Array

To run an algorithm I need to use as input a text file. 要运行算法,我需要使用文本文件作为输入。 Input Example: 输入示例:

105.0,45.0,22.0 105.0,45.0,22.0
240.0,3.0,50.0 240.0,3.0,50.0
4344.0,4.0,55.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) 输出是具有双值的距离矩阵(输入*输入矩阵,因此在这种情况下为3 * 3矩阵)

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. Numberformatexceptions。 Should be somewhere on the parsing I guess. 我猜应该在解析的某个地方。 Help appreciated. 帮助表示赞赏。 Thanks in advance! 提前致谢!

your inputX = stringArr[i].toString(); 您的inputX = stringArr[i].toString(); is apparently being 0 or something that cannot be parsed to Double . 显然是0或无法解析为Double please check that and set some if-condition there if it makes sense for ya.. 请检查并在其中设置一些if条件(如果对您有意义)。

hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM