【发布时间】:2019-02-25 09:11:26
【问题描述】:
我被困在这里...我有一个 txt 文件,其中有几个 (int) 二维数组,由空格分隔。每个二维数组之前的第一行是数组的维度。 (二维数组平方:#rows = #columns = dim)
4
1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
在这个阶段,我希望能够在第一行读取数组的维度 (dim =4)。将文件中的二维数组放入二维数组并显示
如何获得第一行的整数?有什么见解吗? 这是我到目前为止所拥有的:
Scanner scan = new Scanner(new File(fileInput));
dim= 4; //this value should be read from the first line
array = new int[4][4];
while (scan.hasNext()) {
for (int row = 0; row < dim; row++) {
for (int column = 0; column < dim; column++) {
array[row][column] = scan.nextInt();
System.out.print(array[row][column]);
}
System.out.println();
}
}
scan.close();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(array[i][j]);
}
System.out.println();
【问题讨论】:
-
??你知道如何从连续行而不是第一行的文本中读取整数吗?
-
这是正确的!当我尝试输入第一个值 int FirstVal =scan.NextInt() 时,下一行的第一个整数不会被读取!
-
显然这没有任何意义。发布MCVE。