使用 StringTokenizer 将 .txt 文件转换为二维数组
Using StringTokenizer to convert a .txt file into a 2d array
文本文件(twodimension8.txt)。第一行是由','
分隔的行和列
4,6
1,2,3,4,5,6
23,55,34,89,41,72
9,27,19,56,33,82
3,65,21,66,85,11
到目前为止我的代码。
使用 TextFileInput class 从文件中读取
import java.util.*;
public class Tokens {
public static TextFileInput myFile;
public static StringTokenizer myTokens;
public static int[][] twodimarr;
public static String line;
public static void main(String[] args) {
myFile = new TextFileInput("twodimension8.txt");
line = myFile.readLine();
System.out.println("The input line is "+line);
myTokens = new StringTokenizer(line,",");
int row = Integer.parseInt(myTokens.nextToken());
int col = Integer.parseInt(myTokens.nextToken());
twodimarr = new int[row][col];
for (int i = 0; i < row; i++) {
line = myFile.readLine();
for (int j = 0; j < col; j++) {
myTokens = new StringTokenizer(line, ",");
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
for (int i=0; i<row; i++) {
for (int j=0; j<col;j++)
System.out.print(twodimarr[i][j]);
System.out.println();
}
} //main
}
输出:
111111
232323232323
999999
333333
问题似乎出在 for 循环中,它试图将它添加到二维数组中。我不确定如何解决这个问题。
实际的错误在这个块中:
for (int i = 0; i < row; i++) {
line = myFile.readLine();
for (int j = 0; j < col; j++) {
myTokens = new StringTokenizer(line, ",");
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
myTokens
每行只能重新分配一次,而不是每个单独的数字。因此,改为将该位更改为:
for (int i = 0; i < row; i++) {
line = myFile.readLine();
myTokens = new StringTokenizer(line, ",");
for (int j = 0; j < col; j++) {
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
此外,将局部变量存储为静态 class 变量不是好的做法。而不是做
public static TextFileInput myFile;
在 class 定义的开头,在 main
的顶部执行 TextFileInput myFile = new TextFileInput("twodimension8.txt");
。 (其他变量相同)。
解决我上面提到的问题:
public static int[][] loadArray(String path) throws IOException {
int[][] result = null;
try (Scanner s = new Scanner(new File(path))) {
s.useDelimiter("[,\r\n]");
int numRows = s.nextInt();
int numCols = s.nextInt();
result = new int[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
result[row][col] = s.nextInt();
}
}
return result;
}
}
文本文件(twodimension8.txt)。第一行是由','
分隔的行和列4,6
1,2,3,4,5,6
23,55,34,89,41,72
9,27,19,56,33,82
3,65,21,66,85,11
到目前为止我的代码。 使用 TextFileInput class 从文件中读取
import java.util.*;
public class Tokens {
public static TextFileInput myFile;
public static StringTokenizer myTokens;
public static int[][] twodimarr;
public static String line;
public static void main(String[] args) {
myFile = new TextFileInput("twodimension8.txt");
line = myFile.readLine();
System.out.println("The input line is "+line);
myTokens = new StringTokenizer(line,",");
int row = Integer.parseInt(myTokens.nextToken());
int col = Integer.parseInt(myTokens.nextToken());
twodimarr = new int[row][col];
for (int i = 0; i < row; i++) {
line = myFile.readLine();
for (int j = 0; j < col; j++) {
myTokens = new StringTokenizer(line, ",");
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
for (int i=0; i<row; i++) {
for (int j=0; j<col;j++)
System.out.print(twodimarr[i][j]);
System.out.println();
}
} //main
}
输出:
111111
232323232323
999999
333333
问题似乎出在 for 循环中,它试图将它添加到二维数组中。我不确定如何解决这个问题。
实际的错误在这个块中:
for (int i = 0; i < row; i++) {
line = myFile.readLine();
for (int j = 0; j < col; j++) {
myTokens = new StringTokenizer(line, ",");
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
myTokens
每行只能重新分配一次,而不是每个单独的数字。因此,改为将该位更改为:
for (int i = 0; i < row; i++) {
line = myFile.readLine();
myTokens = new StringTokenizer(line, ",");
for (int j = 0; j < col; j++) {
twodimarr[i][j] = Integer.parseInt(myTokens.nextToken());
}
}
此外,将局部变量存储为静态 class 变量不是好的做法。而不是做
public static TextFileInput myFile;
在 class 定义的开头,在 main
的顶部执行 TextFileInput myFile = new TextFileInput("twodimension8.txt");
。 (其他变量相同)。
解决我上面提到的问题:
public static int[][] loadArray(String path) throws IOException {
int[][] result = null;
try (Scanner s = new Scanner(new File(path))) {
s.useDelimiter("[,\r\n]");
int numRows = s.nextInt();
int numCols = s.nextInt();
result = new int[numRows][numCols];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
result[row][col] = s.nextInt();
}
}
return result;
}
}