简体   繁体   English

数组索引超出界限错误输入数据

[英]Array Index Out of Bounds error Inputting Data

I'm trying to input the numbers in a data file into an array. 我正在尝试将数据文件中的数字输入到数组中。 However, I keep II am getting an array index out of bounds error. 但是,我保持II得到数组索引超出界限错误。 Here is the block of code reporting the error. 以下是报告错误的代码块。

Scanner myIn = new Scanner(new FileInputStream(("Data10.txt")));

for(int x=0; x<n.length; x++)   
    for(int j=0;j<s.length();j++)
    {
      n[x][j] = Character.getNumericValue(s.charAt(j));

    }

For j index, you want the length of the second dimension of the array, not the length of the string. 对于j index,您需要数组的第二个维度的长度,而不是字符串的长度。

for(int j=0;j<s.length();j++)

should be 应该

for(int j=0;j<n[i].length;j++)

PS: counters are usually named i,j,k , not x . PS:计数器通常命名为i,j,k ,而不是x This is an inheritance from Fortran. 这是Fortran的继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM