java - Returning and displaying contents of ArrayList in different class? -
i'm working on project tally student's choices , add them count array (still working on part). now, i'm trying retrieve choices have been sent , added student arraylist in student class.
student class:
public class students { private string name; private arraylist<integer> choices = new arraylist<integer>(); public students(){ name = " "; } public students(string name){ name = name; } public void setname(string name){ name = name; } public string getname(){ return name; } public void addchoices(int choices){ choices.add(choices); } public arraylist<integer> getchoices(){ return choices; }
here main driver class:
public class p1driver { public static void main(string[] args) throws ioexception{ arraylist<students> students = new arraylist<students>(); string[] choices = new string[100]; int[] count; 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[choicesindex] = 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 countindex = 0; for(int = 0; < students.size(); i++){ if(students.get(i).getchoices(i) == -1){ } }
the last part now. it's near done (i'm right in middle of it) during construction of loop choices students, i'm getting error says, "the method getchoices() in type students not applicable arguments (int)." can explain means, me error is, , possibly how fix it? all.
getchoices(int i)
not method you've defined.
if(students.get(i).getchoices(i) == -1){ }
getchoices()
returns list, can use get
method on list:
if(students.get(i).getchoices().get(i) == -1){ }
alternatively, make getchoice
method:
public integer getchoice(int i){ return choices.get(i); }
Comments
Post a Comment