[英]How do I import multiple matrices from a file in Java, where the numbers are separated by a space and the matrices are separated by “@”?
如何從Java中的文件導入多個矩陣,其中數字之間用空格分隔,而矩陣之間用不同的字符(例如“ @”)分隔?
這是文件matrix.txt的示例:
2 2
34 78
89 -12
@
2 2
67 76
123 5
@之前是矩陣A,@之后是矩陣B
我在MS Windows(Eclipse)中工作,我嘗試了兩種或三種不同的方式,我的問題是如何處理這兩個定界符並在@之后除以矩陣。
這是我嘗試過的一部分代碼:
public int readFiles(String file) throws IOException {
int A = 0;
String lines = "";
BufferedReader myBuffer = null;
myBuffer = new BufferedReader(new FileReader(file));
int count=0;
lines = myBuffer.readLine();
while(lines!=null) {
count++;
lines = myBuffer.readLine();
}
myBuffer.close();
myBuffer = new BufferedReader(new FileReader(file));
int size = (count/2);
int nRows = (size);
int nCols = (size/2);
String [] [] arrayA = new String [nRows][nCols];
for(int i =0;i<arrayA.length;i++) {
for(int j=0;j<arrayA[i].length-1;j++) {
lines = myBuffer.readLine();
String[] splited = lines.split(" ");
arrayA[i][j]= splited[0];
arrayA[i+1][j+1]= splited[1];
}
}
for(int i =0;i<arrayA.length;i++) {
for(int j=0;j<arrayA[i].length;j++) {
System.out.println(arrayA[i][j]);
}
}
return A;
}
感謝您如此迅速地回答我...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.