java - Instantiating array but keep getting IndexOutOfBounds Exception -
i'm trying tally count array keep set of 50 choices there 3 options each choice. count array should have 150 elements, per instructor (3 x 50 = 150). keep getting indexoutofbounds exception @ line 55 (index = thischoice.get(i)
). i'm thinking must have how (or where?) i'm instantiating count array @
line 50: int[] count = new int[students.get(0).getchoices().size()*3]
because rest of code came instructor , presumably correct. ideas on sending out of bounds?
public class p1driver { public static void main(string[] args) throws ioexception{ arraylist<students> students = new arraylist<students>(); arraylist<string> choices = new arraylist<string>(); scanner scan1 = new scanner(new file("choices.txt")); scanner scan2 = new scanner(new file("eitheror.csv")); // scan first file. int choicesindex = 0; while(scan1.hasnextline()){ string line = scan1.nextline(); choices.add(line); choicesindex++; } scan1.close(); // scan second file. int studentindex = 0; while(scan2.hasnextline()){ string line = scan2.nextline(); string [] splits = line.split(","); students.add(new students(splits[0])); for(int = 1; < splits.length; i++){ students.get(studentindex).addchoices(integer.parseint(splits[i])); } studentindex++; } scan2.close(); // instantiate , add count array. int index, countindex; arraylist<integer> thischoice; int[] count = new int[students.get(0).getchoices().size()*3]; for(int = 0; < students.size(); i++){ countindex = 1; thischoice = students.get(i).getchoices(); for(int j = 0; j < thischoice.size(); j++){ index = thischoice.get(i); count[countindex + index] = count[countindex + index] + 1; countindex+=3; } } // display data. countindex = 1; for(int = 0; < choices.size(); i+=2){ system.out.println(choices.get(i) + count[countindex] + choices.get(i+1) + count[countindex+1] + " invalid: " + count[countindex-1]); countindex+=3; }
hi please check second nested loop, should j
instead of i
. haven't used int j
in loop.
(int = 0; < students.size(); i++) { countindex = 1; thischoice = students.get(i).getchoices(); (int j = 0; j < thischoice.size(); j++) { index = thischoice.get(j); count[countindex + index] = count[countindex + index] + 1; countindex += 3; } }
Comments
Post a Comment