Java 如何将文本文件中的整数读入二维数组?

Java 如何将文本文件中的整数读入二维数组?,java,arrays,parseint,Java,Arrays,Parseint,我在一个文本文件中有整数,都用空格分隔 我知道如何读取文件,但我不知道如何将整数放入数组。我知道我必须在某个地方使用parseInt 阵列为4x4。整数是: 2 1 4 2 9 7 5 3 9 8 3 5 1 0 0 0 我的代码: arr = new int [4][4]; IODialog input = new IODialog(); String location = input.readLine("Enter the full path of the configurat

我在一个文本文件中有整数,都用空格分隔

我知道如何读取文件,但我不知道如何将整数放入数组。我知道我必须在某个地方使用
parseInt

阵列为4x4。整数是:

2 1 4 2  
9 7 5 3  
9 8 3 5  
1 0 0 0
我的代码:

arr = new int [4][4];
IODialog input = new IODialog();
String location = input.readLine("Enter the full path of the configuration text file: ");
Scanner scn = null;
try {
  scn = new Scanner(new BufferedReader(new FileReader(location)));
  int i, j = 0;
  String line = scn.nextLine(); 
  String[] numbers = line.split(" "); 
} finally {
  if (scn != null) {
    scn.close();
   }
}
这会有帮助的

   int[][] a = new int[4][4];
   BufferedReader br = new BufferedReader(new FileReader("path/to/file"));

   for (int i = 0; i < 4; i++) {
        String[] st = br.readLine().trim().split(" ");
        for (int j = 0;j < 4; j++) {
            a[i][j] = Integer.parseInt(st[j]);
        }
   }
int[]a=新的int[4][4];
BufferedReader br=新的BufferedReader(新的文件读取器(“路径/到/文件”));
对于(int i=0;i<4;i++){
字符串[]st=br.readLine().trim().split(“”);
对于(int j=0;j<4;j++){
a[i][j]=整数.parseInt(st[j]);
}
}
这会很有帮助

   int[][] a = new int[4][4];
   BufferedReader br = new BufferedReader(new FileReader("path/to/file"));

   for (int i = 0; i < 4; i++) {
        String[] st = br.readLine().trim().split(" ");
        for (int j = 0;j < 4; j++) {
            a[i][j] = Integer.parseInt(st[j]);
        }
   }
int[]a=新的int[4][4];
BufferedReader br=新的BufferedReader(新的文件读取器(“路径/到/文件”));
对于(int i=0;i<4;i++){
字符串[]st=br.readLine().trim().split(“”);
对于(int j=0;j<4;j++){
a[i][j]=整数.parseInt(st[j]);
}
}

你走对了方向

只需在读取扫描仪的下一行内容后立即填充数组即可

 int[][] arr = new int[4][4];
 String location = input.readLine("Enter the full path of the configuration text file: ");
 scn = new Scanner(new BufferedReader(new FileReader(location)));

 String line = scn.nextLine(); 
 String[] numbers = line.split(" "); 

 for(int i = 0; i < 4; i++)
     for(int j = 0; j < 4; j++)
         arr[i][j] = Integer.parseInt(numbers[j]); 
int[]arr=newint[4][4];
String location=input.readLine(“输入配置文本文件的完整路径:”);
scn=新的扫描仪(新的BufferedReader(新的文件阅读器(位置));
String line=scn.nextLine();
字符串[]数字=行。拆分(“”);
对于(int i=0;i<4;i++)
对于(int j=0;j<4;j++)
arr[i][j]=整数.parseInt(数字[j]);

这将逐行读取输入,并在每行中插入整数以完成4x4数组

你在正确的轨道上

只需在读取扫描仪的下一行内容后立即填充数组即可

 int[][] arr = new int[4][4];
 String location = input.readLine("Enter the full path of the configuration text file: ");
 scn = new Scanner(new BufferedReader(new FileReader(location)));

 String line = scn.nextLine(); 
 String[] numbers = line.split(" "); 

 for(int i = 0; i < 4; i++)
     for(int j = 0; j < 4; j++)
         arr[i][j] = Integer.parseInt(numbers[j]); 
int[]arr=newint[4][4];
String location=input.readLine(“输入配置文本文件的完整路径:”);
scn=新的扫描仪(新的BufferedReader(新的文件阅读器(位置));
String line=scn.nextLine();
字符串[]数字=行。拆分(“”);
对于(int i=0;i<4;i++)
对于(int j=0;j<4;j++)
arr[i][j]=整数.parseInt(数字[j]);

这将逐行读取输入,并在每行中插入整数以完成4x4数组

即使是年轻的巫师也必须演示一些初始工作-你有多远?即使是年轻的巫师也必须演示一些初始工作-你有多远?