java - Reading ints into a multidimensional array -
my problem occurs during loops read in values file scores array. program reads in , prints first 6 values or 2 lines of ints, arrayindexoutofboundsexception: 2
i have no idea why stopping there. if have j<1000, read 17 values. anyway, file i'm reading in below (wasn't sure formatting text file).
any appreciated
andy matt tom 3 2 3 4 4 5 3 3 2 2 2 2 2 4 2 2 3 2 4 4 5 2 3 3 4 3 5 3 3 6 2 2 5 3 3 3 3 3 2 3 2 4 3 2 6 3 4 3 2 3 2 2 2 2 50 52 62 public static void main( string args[] ) { try { if (args.length<1) { system.out.printf("\n...you must enter name of file\n"); system.exit(0); } scanner infile = new scanner ( new file(args[0]) ); int par= 3; int play= 18; string[] players= new string[play]; int k=0; int scores[][]= new int[play-1][par-1]; while(infile.hasnext()) { players[k]=infile.next(); k++; if (k==play) break; } for(int j=0; j<par; j++) { (int i=0; i<play; i++) { scores[j][i]=infile.nextint(); system.out.println(scores[j][i]); } } } catch (filenotfoundexception e) { system.out.println("bug"); system.exit(0); } }
actually, there 3 issues.
- there 3 players, not 18
- you need 18x3 array, not 17x2 array
[i][j]
instead of[j][i]
diff of code against modified version (which works charm):
22c22 < string[] players= new string[play]; --- > string[] players= new string[par]; 24c24 < int scores[][]= new int[play-1][par-1]; --- > int scores[][]= new int[play][par]; 32c32 < if (k==play) --- > if (k==par) 41,42c41,42 < scores[j][i]=infile.nextint(); < system.out.println(scores[j][i]); --- > scores[i][j]=infile.nextint(); > system.out.println(scores[i][j]);
Comments
Post a Comment