java - Array Index Out of Bounds Error While Populating From a Text File -
i having trouble error trying populate array text file. think might have if
loop, i'm not sure. it's giving me out of bounds error on mydata[9]
. want check see if first letter d
(meaning following stuff update), , make update object. update.txt
file has 10 things in populate mydata
with, why confused why mydata[9]
giving me out of bounds error (specifically try set equal lastthirty
).
scan = new scanner(file); while (scan.hasnext()) { string str = scan.nextline(); string[] mydata = str.split("#"); if (mydata[0].equalsignorecase("d")) { recordtype = mydata[0]; actioncode = mydata[1]; boxid = integer.parseint(mydata[2]); movieid = integer.parseint(mydata[3]); movietitle = mydata[4]; moviegenre = mydata[5]; releaseyear = mydata[6]; instock = mydata[7]; totalrentals = integer.parseint(mydata[8]); lastthirty = integer.parseint(mydata[9]); updates[count] = new updaterecord(recordtype, actioncode, boxid, movieid, movietitle, moviegenre, releaseyear, instock, totalrentals, lastthirty); count++; }
the content of file follows:
h#title town video mart updates#04\24\2016 d#a#4#5#harry potter#action#2001#true#50#5 d#a#4#5#shutter island#suspense#2001#true#50#5 d#a#4#5#the blind side#drama#2001#true#50#5 d#a#4#5#borat#comedy#2001#true#50#5 d#a#4#5#bad grandpa#comedy#2001#true#50#5 t#title town video mart updates#04\24\2016#5
it depends on how txt written, string[] mydata = str.split("#");
return array long 9, accessing 10nth element cause error.
consider example "boo#and#foo" should return string[3]. txt functional should return string[10].
try modify txt add '#' , see if works, or better see content of array in debug.
Comments
Post a Comment