public static void main(String args[]){
new Converter();
//the converter() method reads a csv file that pass it to an array of String
// called stringList; where in stringList is a static variable;
ArrayList<Student> list = new ArrayList<Student>(5);
String p[] = null;
Student stud = null;
for(int z = 0; z < stringList.length; z++){
p = stringList[z].split(",");
id = Integer.parseInt(p[0]);
yr = Integer.parseInt(p[5]);
fname = p[1];
gname = p[2];
mname = p[3].charAt(0);
course = p[4];
stud = new Student(id,yr,fname,gname,mname,course);
studs = stud;
我试图显示上面变量的当前值并将它们与学生对象进行比较
System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should be the same with : " +stud.toString());
list.add(studs); // add the student object to the list
} //end of the for loop
然后我注意到我的 Arraylist 只显示一个值:
for (int c = 0; c<list.size(); c++){
System.out.println(" @list "+c+": "+list.get(c));
}
} // end of main method
通常我会阅读 100 多个项目,但在这个例子中我只读了 5 个;这是程序的输出;
2123259 1 AVILA JEREMY RAYMUND T BSIT should be the same with : 2123259,AVILA,JEREMY RAYMUND,T,BSIT,1
2124919 1 BEROÑA MAYNARD W BSIT should be the same with : 2124919,BEROÑA,MAYNARD,W,BSIT,1
2124679 1 CABRERA JERSON RHOD D BSIT should be the same with : 2124679,CABRERA,JERSON RHOD,D,BSIT,1
2124905 1 CABRILLAS ARMANDO JR. B BSIT should be the same with : 2124905,CABRILLAS,ARMANDO JR.,B,BSIT,1
2123400 1 CARIÑO JVANZ S BSIT should be the same with : 2123400,CARIÑO,JVANZ,S,BSIT,1
@list 0: 2123400,CARIÑO,JVANZ,S,BSIT,1
@list 1: 2123400,CARIÑO,JVANZ,S,BSIT,1
@list 2: 2123400,CARIÑO,JVANZ,S,BSIT,1
@list 3: 2123400,CARIÑO,JVANZ,S,BSIT,1
@list 4: 2123400,CARIÑO,JVANZ,S,BSIT,1
现在,女士们先生们,我的问题是我试图显示 Stud 对象以检查它是否读取正确的值并且确实正确显示它,然后在为其分配值之后,然后将其添加到列表中。但是有些事情让我感到困惑,我无法弄清楚为什么我的列表只包含数组中的最后一项?请帮帮我,我被困了两个晚上。
这是我的全部代码,我专注于主要
公共类转换器{
private static ArrayList<Student> students;
private static String[] stringList;
private static Scanner fr;
private static int size;
private static int id;
private static int yr;
private static String fname;
private static String gname;
private static char mname;
private static String course;
public static void main(String args[]){
ArrayList<Student> list = new ArrayList<Student>(5);
new Converter();
String p[] = null;
Student stud = null;
students = new ArrayList<Student>(stringList.length);
for(int z = 0; z < stringList.length; z++){
p = stringList[z].split(",");
id = Integer.parseInt(p[0]);
yr = Integer.parseInt(p[5]);
fname = p[1];
gname = p[2];
mname = p[3].charAt(0);
course = p[4];
stud = new Student(id,yr,fname,gname,mname,course);
System.out.println(id +" "+ yr +" "+ fname + " "+gname + " "+mname + " "+course +" should be the same with : " +stud.toString());
list.add(stud);
} // end of for loop
for (int c = 0; c<list.size(); c++){
System.out.println(" @list "+c+": "+list.get(c));
} // end of second loop
}// end of main
public Converter(){
readStudtentsCSV();
id = 0;
yr = 0;
fname = "";
gname = "";
mname = ' ';
course = "";
}
public static void readStudtentsCSV() {
int s = 0;
try {
size = countLines("test.csv");
stringList = new String[size];
fr = new Scanner(new File("test.csv"));
} catch(Exception e){
e.printStackTrace();
}
while (fr.hasNextLine()){
stringList[s] = fr.nextLine();
//System.out.println(stringList[s]);
s++;
}
}
public static int countLines(String eFile) throws Exception{
Scanner fScan = new Scanner(new File(eFile));
int count =0;
while (fScan.hasNextLine()){
count += 1;
String line = fScan.nextLine();
}
fScan.close();
return count;
}
}