How to print 2D Array from .txt file in Java -
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.util.stringtokenizer; public class csvimport5 { public static void main(string[] args) throws ioexception { double [][] data = new double [87][2]; file file = new file("buydata.txt"); int row = 0; int col = 0; bufferedreader bufrdr = new bufferedreader(new filereader(file)); string line = null; //read each line of text file while((line = bufrdr.readline()) != null && row < data.length) { stringtokenizer st = new stringtokenizer(line,","); while (st.hasmoretokens()) { //get next token , store in array data[row][col] = double.parsedouble(st.nexttoken()); col++; } col = 0; row++; } system.out.println(" "+data[87][2]); } }
error message:
arrayindexoutofbounds exception @ "system.out.println(" "+data[87][2]);
my txt file is:
8.00,28.00 18.00,28.00 8.00,23.00 12.00,20.00 15.00,30.00 12.00,32.00 12.00,20.00 18.00,31.00 29.00,25.00 6.00,28.00 7.00,28.00 6.00,24.00 14.00,30.00 11.00,23.00 12.00,20.00 31.00,24.00 11.00,20.00 17.00,23.00 14.00,32.00 15.00,23.00 8.00,20.00 17.00,31.00 7.00,20.00 12.00,23.00 15.00,20.00 12.00,20.00 21.00,20.00 27.00,27.00 18.00,20.00 25.00,27.00 46.00,13.00 26.00,10.00 47.00,22.00 44.00,14.00 34.00,4.00 34.00,4.00 44.00,7.00 39.00,5.00 20.00,0.00 43.00,11.00 43.00,25.00 34.00,2.00 25.00,10.00 50.00,9.00 25.00,9.00 39.00,2.00 34.00,7.00 44.00,15.00 36.00,3.00 40.00,5.00 49.00,21.00 42.00,7.00 35.00,1.00 30.00,2.00 31.00,13.00 53.00,12.00 40.00,4.00 26.00,4.00 50.00,55.00 57.00,51.00 62.00,52.00 56.00,52.00 59.00,40.00 61.00,68.00 66.00,49.00 57.00,49.00 62.00,58.00 47.00,58.00 53.00,40.00 60.00,54.00 55.00,48.00 56.00,65.00 67.00,56.00 55.00,43.00 52.00,49.00 67.00,62.00 68.00,61.00 65.00,58.00 46.00,53.00 46.00,49.00 47.00,40.00 64.00,22.00 64.00,54.00 63.00,64.00 63.00,56.00 64.00,44.00 63.00,40.00
double [][] data = new double [87][2];
create double[][] size 87 , 2.
the index reference these entries 86 , 1 (an array starts index "0" , not "1")
try:
system.out.println(" "+data[86][1]);
that should work!
Comments
Post a Comment