我正在研究一个项目,在该项目中,我将选择学生的选择并将其添加到计数数组中(仍在此部分工作)。目前,我正在尝试检索已发送并添加到我的学生班的学生arraylist的选择。
学生课:
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;
}
这是我的主要驱动程序类:
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 the first file.
int choicesIndex = 0;
while(scan1.hasNextLine()){
String line = scan1.nextLine();
choices[choicesIndex] = line;
choicesIndex++;
}
scan1.close();
// Scan the second file.
int studentIndex = 0;
while(scan2.hasNextLine()){
String line = scan2.nextLine();
String [] splits = line.split(",");
students.add(new Students(splits[0]));
for(int i = 1; i < splits.length; i++){
students.get(studentIndex).addChoices(Integer.parseInt(splits[i]));
}
studentIndex++;
}
scan2.close();
// Instantiate and add to the count array.
int countIndex = 0;
for(int i = 0; i < students.size(); i++){
if(students.get(i).getChoices(i) == -1){
}
}
最后一部分是我现在所在的地方。很明显(我正处于中间)尚未完成,但是在我构建一个for loop以从学生那里获得选择时,我会遇到一个错误,> "方法getchoices()在类型中,学生不适用于参数(int)。" 有人可以解释这意味着什么,我的错误在哪里以及如何修复它?谢谢大家。
getChoices(int i)
不是您定义的方法。
if(students.get(i).getChoices(i) == -1){
}
getChoices()
返回列表,因此您可以使用列表上的get
方法:
if(students.get(i).getChoices().get(i) == -1){
}
或者,制作getChoice
方法:
public Integer getChoice(int i){
return choices.get(i);
}
您是否尝试过getChoices()[i]而不是getChoices(i)