我有一个矩阵txt文件(11x25)。
0.611104586 0.421137301 0.148175838 0.093093778 0.528511286 0.418664735 0.501390139 0.02206688 0.103546871 0.633455719 0.265081895 0.451747788 0.329214309 0.60249002 0.662946576 0.44162406 0.371196059 0.084083877 0.589799899 0.18569671 0.034511745 0.302195181 0.278790488 0.031898972 0.697455224
0 0.126780289 0.744394666 0.48235826 0.757190046 0 0.982900393 0 0.428819183 0.341336135 0.248345722 0.340234421 0 0.505015206 0.844733493 0 0.520242556 0.839144273 0.944768681 0 0 0.911173675 0.96852497 0.513269179 0
0.719522104 0.904803048 0 0.438443555 0.619019934 0.770586942 0.035069914 0.284726903 0 0.900156226 0 0.614178016 0.756121647 0 0.482162641 0.346972852 0 0.530160102 0.607970986 0 0 0.70776022 0.630313049 0.537430321 0.429637915
0.965552324 0.237316539 0 0.675540672 0.02245449 0 0.906837513 0.046865469 0.985370941 0 0 0.581136159 0 0.731418151 0.410065655 0.185012435 0.810102943 0.061834628 0.760408343 0.52844337 0.106327038 0.764641701 0 0.241043197 0.138591668
0.785265388 0.245375911 0.003825113 0.392088166 0.746347629 0 0.865455277 0 0.320064792 0.469631124 0.275800621 0.226402945 0.196849125 0.3767231 0 0.504222038 0.252534767 0 0.108825926 0.038854648 0 0 0.837990169 0.233463218 0.973794244
0 0.369290247 0.391632502 0.966710703 0.456646601 0.744378787 0.500206874 0.429575161 0.4157577 0.682093427 0 0 0.88518823 0.545062518 0.844007009 0.209436674 0.532083535 0.643254912 0.022206966 0.972272975 0.207677119 0.619557286 0.684530833 0.039768814 0.566273215
0.719277709 0.370218186 0 0.528553716 0 0.474043295 0.644709559 0.096042971 0.078666296 0.667435055 0.440727479 0.859297227 0.18165689 0.068277826 0.289860387 0.208112415 0.626884285 0.295818382 0.756532711 0 0.470774563 0.560703247 0.040089639 0 0.910059573
0.350135065 0.250992266 0 0.762270073 0.649714246 0.382709691 0 0.434548143 0.096899503 0.221095404 0.207416989 0 0.142452936 0.063563437 0.519824118 0.318727424 0.033631564 0.34535148 0 0.050335104 0 0.085647088 0 0.970307046 0.489823431
0.549077625 0.786267574 0.310947619 0 0.855403201 0.356727854 0.448585797 0.858197183 0.164926857 0.632894281 0.883520503 0.477562449 0 0 0.054450728 0.581420285 0.405555966 0 0.595676984 0.581284627 0.294128393 0 0.022840426 0.442097113 0.168690247
0.773515517 0.667867116 0 0.384918608 0.495617591 0.960445903 0 0.521981671 0.582565937 0.000392407 0.729931754 0.003648767 0 0.959536544 0.308279485 0 0.992524156 0.078062129 0 0.425848147 0 0.146881857 0.488967009 0.914247178 0.556155216
0.20589258 0.113521908 0.215750666 0.241288747 0.803876238 0 0.219172738 0 0 0.560754999 0 0.480321026 0.21864147 0 0.756257475 0.99148594 0.795855095 0.947866607 0.642933671 0.988902986 0 0 0.865736296 0.745107083 0.125387319
尝试在矩阵[i] [j]中以矩阵形式读取,存储和打印,但它始终无法正确读取和打印。这是我在Java中的代码的一部分
static double [][] matrix = new double[20][50];
FileReader fr = new FileReader("TM.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
int lineCount = 0;
while ((line = br.readLine()) != null)
{
String[] numbers = line.split(" ");
for ( int i = 0 ; i < 25 ; i++) {
matrix[lineCount][i] = Double.parseDouble(numbers[i].trim());
System.out.println(matrix[lineCount][i] + " " + i + " " +lineCount);
}
lineCount++;
}
System.out.println(matrix[1][5] + " ");//Note 2
for ( int i = 0; i < 10; i++){
for (int j = 0; j < 25; j++){
System.out.println(matrix[i][j] + " ");
}
}//Note 3.
注意1.错误信息:(已解决。谢谢:))
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
i cannot be resolved to a variable
注意2.无论我放入矩阵中的是什么,总是打印出0.0,例如矩阵[1] [2]。
注意3.我想打印出矩阵,但它给出全部0。
注2和3的问题仍然存在。不确定我是否需要存储txt文件中的矩阵读数才能使用它。
感谢您的帮助!
答案 0 :(得分:2)
使用for
分隔{}
循环的正文,因为当前System.out.println(....)
语句不在循环中。
例如:
for ( int i = 0 ; i < 25 ; i++){
matrix[lineCount][i] = Double.parseDouble(numbers[i]);
System.out.println(matrix[lineCount][i] + " ")
}
答案 1 :(得分:1)
我无法解析变量。
while ((line = br.readLine()) != null)
{
String[] numbers = line.split("\\W+");
for ( int i = 0 ; i < 25 ; i++){
matrix[lineCount][i] = Double.parseDouble(numbers[i].trim());
System.out.println(matrix[lineCount][i] + " " + i + " " +lineCount);
}//Note 1
lineCount++;
}
还有一个是
static double [][] matrix = new double[20][25];//just increase the size