我想从这个文件文本中获取行和列的长度。我怎样才能做到这一点?谢谢!
in.txt
2 1 1 8
1 1 4 15
0 3 2 9
public static void main(String[] args) {
int[][] arr = new int[3][4];
File inputFile = new File("in.txt");
try {
Scanner scanF = new Scanner(inputFile);
for (int i = 0; i < 3; i++) {
String line = scanF.nextLine();
String valVar[] = line.split(" ");
for (int j = 0; j < 4; j++) {
arr[i][j] = Integer.parseInt(valVar[j]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
您也可以使用缓冲读卡器。
int maxRows = 0;
int maxColumn = 0;
try {
Scanner scanF = new Scanner(inputFile);
String line = scanF.nextLine();
while (line != null) {
if (line.trim().length() > 0) {
maxRows++;
String valVar[] = line.split(" ");
if (maxColumn < valVar.length) {
maxColumn = valVar.length;
}
}
try {
line = scanF.nextLine();
} catch (NoSuchElementException e) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}