Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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
Java—如何将文件的下一列放入数组_Java_Arrays_File - Fatal编程技术网

Java—如何将文件的下一列放入数组

Java—如何将文件的下一列放入数组,java,arrays,file,Java,Arrays,File,以下是我正在阅读的文件: >18 64 94 54 34 44 >40 26 26 92 96 34 >56 24 40 92 70 58 >92 72 12 46 46 56 >50 28 2 64 12 58 >98 28 40 88 86 20 >46 56 100 60 52 12 >82 70 98 18 50 30 >58 36 98 4 74

以下是我正在阅读的文件:

>18  64  94  54  34  44
>40  26  26  92  96  34
>56  24  40  92  70  58
>92  72  12  46  46  56
>50  28  2  64  12  58
>98  28  40  88  86  20
>46  56  100  60  52  12
>82  70  98  18  50  30
>58  36  98  4  74  76
>76  28  72  74  74  60
这是我的密码:

>package exercise;
>
>import java.io.File;
>import java.io.FileNotFoundException;
>import java.util.Scanner;
>
>public class Lab1 {
>
>   public static void main(String[] args) throws FileNotFoundException {
>       
>       File file = new File("numberData.csv");
>       Scanner fileScan = new Scanner(file);
>       
>       int[] ND1Column1 = new int[10];
>       int[] ND1Column2 = new int[10];
>       int[] ND1Column3 = new int[10];
>       int[] ND1Column4 = new int[10];
>       int[] ND1Column5 = new int[10];
>       int[] ND1Column6 = new int[10];
>       int[] ND2Column1 = new int[16];
>       int[] ND2Column2 = new int[16];
>       int[] ND2Column3 = new int[16];
>       int[] ND2Column4 = new int[16];
>       
>       
>       for(int row = 0; row < 10; row++)
>       {
>           ND1Column1[row] = fileScan.nextInt();
>           fileScan.nextLine();
>       }
>       for(int row = 0; row < 10; row++)
>       {   
>           fileScan.nextInt();
>           ND1Column2[row] = fileScan.nextInt();
>           fileScan.nextLine();
>       }
>       
>       File file2 = new File("numberData2.csv");
>       Scanner fileScan2 = new Scanner(file);
>       
>       System.out.print(ND1Column2[0]);
>   }

我成功地将第一列放入数组。我只需要弄清楚如何将以下列放入它们自己的数组中。我这里为第二个for循环编写的代码似乎没有正确执行。任何帮助都将不胜感激。

您可以去掉第二个for循环,并将代码合并到一个嵌套的for循环中

通过将文件读入二维数组(矩阵),也可以节省大量代码。下面是如何编写矩阵:

int[][] ND1Column = new int[6][10];
int[][] ND2Column = new int[16][10];

for (int row = 0; row < 6; row++) {
    for (int col = 0; col < 10; col++) {
        if (!fileScan.hasNextInt()) fileScan.nextLine();
        ND1Column[row][col] = fileScan.nextInt();
    }
}

//Do the same thing for ND2Column except give the array 16 cols and 4 rows

试着这样做:

BufferedReader br = new BufferedReader(new FileReader("numberData.csv"));
String line;
int i = 0;
while ((line = br.readLine()) != null) {
   i++;
   switch(i):
   case 1:
      // put line inside ND1Column1
   case 2:
      // put line inside ND1Column2
   //etc
}
br.close();
似乎不知道你怎么知道?