Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays java中的文件到数组。这个代码有什么问题?_Arrays_File - Fatal编程技术网

Arrays java中的文件到数组。这个代码有什么问题?

Arrays java中的文件到数组。这个代码有什么问题?,arrays,file,Arrays,File,我不明白这个代码有什么问题。我想读txt文件 11,10 2,20 放入java中的数组[] 我的代码是 import java.util.Arrays; class ArrayMatrix{ public static void main (String[] args) { int rows = 2; int cols = 2; FileInput in = new FileInput("test.txt"); int[][] circle = new int

我不明白这个代码有什么问题。我想读txt文件

11,10
2,20
放入java中的数组[]

我的代码是

import java.util.Arrays;
class ArrayMatrix{
   public static void main (String[] args) {
   int rows = 2; 
   int cols = 2;
   FileInput in = new FileInput("test.txt");
   int[][] circle = new int[rows][cols]; 
   int i = 0; 
   int j = 0; 
   String [] line;
   for(i = 0; i<rows; i++) { 
       line = in.readString().split(",");
       for(j = 0; j<cols; j++) { 
           circle[i][j] = Integer.parseInt(line[j]);
           System.out.println(circle[i][j]);
       }
    }
}
}
你能帮我理解为什么吗

我想要输出

11 10
2 20

为什么要尝试使用comas拆分字符串,而不是在每行上使用comas分隔值

line = in.readString().split(",");  //this is expecting coma separated values on each line
另外,System.out.println()在末尾添加了一行新行。这就是为什么它们以不同的行结束。改为使用System.out.print(圈[i][j])。

使用
println()
时,每个元素都写在新行上。尝试:

   for(j = 0; j<cols; j++) { 
       circle[i][j] = Integer.parseInt(line[j]);
       System.out.print(circle[i][j] + " ");
   }
   System.out.println();

<代码> >(j=0;j你的输出是这样的,因为你只使用Soop.Out.PrdLn(Curr[i])),中间的命令只打印到一行。使用Soal.Out.Posid命令有助于正确打印。如下面的更改代码示例

所示
import java.util.Arrays;
class ArrayMatrix{
   public static void main (String[] args) {
   int rows = 2; 
   int cols = 2;
   FileInput in = new FileInput("test.txt");
   int[][] circle = new int[rows][cols]; 
   int i = 0; 
   int j = 0; 
   String [] line;
   for(i = 0; i<rows; i++) { 
       line = in.readString().split(",");
       for(j = 0; j<cols; j++) { 
           circle[i][j] = Integer.parseInt(line[j]);
           System.out.print(circle[i][j] + " ");
       }
       System.out.println();
    }
}
}
导入java.util.array;
类数组矩阵{
公共静态void main(字符串[]args){
int行=2;
int cols=2;
FileInput in=newfileinput(“test.txt”);
int[][]圆圈=新的int[行][cols];
int i=0;
int j=0;
字符串[]行;

对于(i=0;ii如果您对自己的答案感到满意,您应该接受最好的答案(通过单击答案旁边的勾号,将其变为绿色)
import java.util.Arrays;
class ArrayMatrix{
   public static void main (String[] args) {
   int rows = 2; 
   int cols = 2;
   FileInput in = new FileInput("test.txt");
   int[][] circle = new int[rows][cols]; 
   int i = 0; 
   int j = 0; 
   String [] line;
   for(i = 0; i<rows; i++) { 
       line = in.readString().split(",");
       for(j = 0; j<cols; j++) { 
           circle[i][j] = Integer.parseInt(line[j]);
           System.out.print(circle[i][j] + " ");
       }
       System.out.println();
    }
}
}