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;
I tried to display the current values of the variables above and compare them to the student object
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
Then I noticed my Arraylist only displays only one value:
for (int c = 0; c<list.size(); c++){
System.out.println(" @list "+c+": "+list.get(c));
}
} // end of main method
Normally I would read 100+ items but with this example I made it only 5; this is the out put of the program;
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
Now my problem here ladies and gentlemen is that I tried to display the Stud object inorder to check if its reading the correct values and indeed it displays it correctly, and then after assigning values to it , I then add it to the list. but something confuses me , and I couldnt figure out why my list only contains the last item in my array? please help me, I've been stuck for 2 nights.
this my whole code , I focus on the main
public class Converter {
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;
}
}