简体   繁体   中英

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. 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.

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 . This is an inheritance from Fortran.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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