java - Index out of bounds error when parsing to Integer -
i getting error when trying parse string
integer
or double
.
int id = integer.parseint(stringparts[2]);
if print stringparts[2]
works, throws error when parsing.
this complete loop i'm using:
public static studentrecord[] creates(string filename) throws ioexception { bufferedreader br = new bufferedreader(new filereader("/users/dna40/desktop/lab11input.txt")); int linetext = linecount("/users/dna40/desktop/lab11input.txt"); string record; string cons = ("[ ]"); studentrecord[] student = new studentrecord[linetext]; string[] stringparts = new string[5]; for(int = 0; < linetext ; i++){ student[i] = new studentrecord();//creates object class record = br.readline(); //stores first line of text file stringparts = record.split("\\s+");//splits line parts student[i].setfirstname(stringparts[0]); student[i].setlastname(stringparts[1]); int id = integer.parseint(stringparts[2]); student[i].setid(id); double gpa = double.parsedouble(stringparts[3]); student[i].setgpa(gpa); int hours = integer.parseint(stringparts[4]); student[i].sethours(hours); } return student; } public static int linecount(string filename) throws ioexception { bufferedreader br = new bufferedreader(new filereader(filename)); int count = 0; string currentline; while ((currentline = br.readline()) != null){ count++; } return count; }
it generates error: exception in thread "main" java.lang.arrayindexoutofboundsexception: 2
thank you
index out of bounds means that
stringparts[2]
does not exist. check length of array. split isent working expect
Comments
Post a Comment