下午好,
我目前正在阅读格式为
5 5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
成二维数组。
第一行是二维数组的行长和列长(即 5x5)。
前面的行给出了输入值(值本身并不重要,只是它们是整数)需要读入二维数组,使得 array[0][0] = 0,array[0][1] = 0 等.
我目前最讨厌的是在第一行之后读取文件的内容并显示它到目前为止我所拥有的是,
public static void importFile(String fileName) throws IOException
{
int rows = 0;
int cols = 0;
int[][] numArray = null;
try {
int count = 0;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null)
{
count++;
if (count == 1)
{
String[] tokenizer = line.split("\\s+");
rows = Integer.parseInt(tokenizer[0]);
System.out.println(rows);
cols = Integer.parseInt(tokenizer[1]);
System.out.println(cols);
numArray = new int[rows][cols];
} // end of if statement
else if(count > 1)
{
String[] tokenizer = line.split(" ");
for(int j = 0; j < tokenizer.length; j++)
{
numArray[rows][j] = Integer.parseInt(tokenizer[j]);
System.out.print(numArray[rows][j] + " ");
}
System.out.println("");
rows++;
} // end of else if
}// end of while loop
} //end of try statement
catch (Exception ex) {
System.out.println("The code throws an exception");
System.out.println(ex.getMessage());
}
System.out.println("I am printing the matrix: ");
for (int i = 0; i < rows; i++) {
for(int j=0; j < cols; j++)
System.out.print(numArray[i][j] + " ");
System.out.println("");
}
} // end of import file
} // 类结束输出如给定
Please enter the file you'd like to use:
data4.txt
5
5
The code throws an exception
5
I am printing the matrix:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0