Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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 - Fatal编程技术网

在java中,如何在插入到二维数组之前限制从文件中读取的字符数和行数

在java中,如何在插入到二维数组之前限制从文件中读取的字符数和行数,java,arrays,Java,Arrays,我已经阅读了前面的答案,但与我所寻找的完全不同,特别是在java中。这是我的代码,我的代码块只能读取一个单字符整数,这不是我想要做的,我想要读取多个字符整数,它不起作用。我希望从文件中一行只读取16行和16个整数,即使文件包含超过16行和每行超过16个整数。有人能和我分享一个想法吗 以下是示例输入数据: 13 20 0 0 0 0 0 0 0 0 0 0 11 2 0 0 0 0 0 0 0 0 0 4 5 0 0 11 2 0 0 0 0 0 0 0 0 0 333 4 0 0 0 0 0

我已经阅读了前面的答案,但与我所寻找的完全不同,特别是在java中。这是我的代码,我的代码块只能读取一个单字符整数,这不是我想要做的,我想要读取多个字符整数,它不起作用。我希望从文件中一行只读取16行和16个整数,即使文件包含超过16行和每行超过16个整数。有人能和我分享一个想法吗

以下是示例输入数据:

13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4  0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8

0 0 0 0 0 0 0 0 0 0 11 2 333 4 
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4  0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4 
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4  0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4 
13 20 0 0 0 0 0 0 0 0 0 0 11 2
0 0 0 0 0 0 0 0 0 4 5 0 0 11 2
0 0 0 0 0 0 0 0 0 333 4  0 0 0 0
0 0 0 0 0 0 0 0 0 9 10 41 3 5 8
0 0 0 0 0 0 0 0 0 0 11 2 333 4 
我只想把它插入到二维数组中,正如您在我的代码中看到的,但是我的数组是16X16,但是文件的大小可能超过16X16,但是我只想读取16X16,即使文件包含的内容超过16X16,并且忽略空行,即使它存在

BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
String line = null;
int[][] board = new int[16][16]; 
int k = 0; 
while((line = bufferedReader.readLine())!=null) { 
String[] newmatrix = line.split(" "); 
for(int i=0; i<9; i++) { 
board[k][i] = Integer.parseInt(newmatrix[i]); 
} 
k++;
 } 
BufferedReader BufferedReader=new BufferedReader(new FileReader(“text.txt”);
字符串行=null;
int[][]板=新int[16][16];
int k=0;
而((line=bufferedReader.readLine())!=null){
字符串[]newmatrix=line.split(“”);
对于(int i=0;i而言,这似乎是一个“学习练习”。因此,仅提示/建议1:

  • 要在16行之后停止读取,请使用计数器

  • 通过测试空行跳过空行

  • 使用
    扫描仪
    hasnetint()
    nextInt()
    处理每一行

  • 避免将文字常量硬连接到代码中是一个好主意……比如
    9
    ,您似乎已经把它从空中拉了出来

  • 使用
    board.length-1
    board[i].length-1
    作为“循环”时的数组边界(请参见上文)

  • 此外,您的输入文件似乎每行只有14个整数,而不是问题所述的16个整数



    1…因为您将通过自己编写代码了解更多信息。

    下面的代码应该可以工作

    BufferedReader bufferedReader = new BufferedReader(new FileReader("text.txt"));
    String line = null;
    int[][] board = new int[16][16]; 
    int k = 0; 
    while((line = bufferedReader.readLine())!=null) { 
        String[] newmatrix = line.split(" "); 
        for(int i = 0; i < 16; i++) { 
            board[k][i] = Integer.parseInt(newmatrix[i]); 
        } 
        k++;
        if (k == 16)
            break;
    }
    bufferedReader.close();
    
    BufferedReader BufferedReader=new BufferedReader(new FileReader(“text.txt”);
    字符串行=null;
    int[][]板=新int[16][16];
    int k=0;
    而((line=bufferedReader.readLine())!=null){
    字符串[]newmatrix=line.split(“”);
    对于(inti=0;i<16;i++){
    board[k][i]=Integer.parseInt(newmatrix[i]);
    } 
    k++;
    如果(k==16)
    打破
    }
    bufferedReader.close();
    
    能否请您详细说明您试图实现的目标。请提供示例输入、所需输出以及当前实现中出现的问题等详细信息。是的,当然,以下是示例输入数据: