-1
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;
    }

}

misserandety
  • 122
  • 1
  • 4
  • 13

2 Answers2

5

The problem is that your fields in the Student class are static. You haven't shown the code, but it could be guessed as this:

public class Student {

    private static int id;
    //other fields...
    //constructor...
    //getters and setters...
}

Just remove the static marker from the fields in this class.

public class Student {

    //field must not be static
    private int id;

    //other non-static fields...
    //constructor...
    //getters and setters...
}

Note that removing the static marker in the fields of Converter class won't fix the problem.

In short, static fields belong to the class, while non-static fields belong to the class object reference. If having static fields in your Student class, all the object references will share this value. When having non-static fields, since they belong to each instance of the class, every object reference will have a different value.

More info: Understanding Instance and Class Members

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

You're overwriting the variables id, yr, fname, ... constantly on each loop iteration. These variables are probably also directly used as fields in the Student class. Try to use a fresh variable each loop iteration.

for(int z = 0; z < stringList.length; z++){
    p = stringList[z].split(",");

    int id = Integer.parseInt(p[0]);

    int yr = Integer.parseInt(p[5]);

    String fname = p[1];

    String gname = p[2];

    char mname = p[3].charAt(0);

    String course = p[4];

    Student 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);    // add the student object to the list

}  //end of the for loop
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • The problem is the data in `Student` class, not the `static` fields in `Converter` class. – Luiggi Mendoza Jul 10 '13 at 22:22
  • There's no reason to even use static fields in this case. Secondly, the problem actually are the static fields. The reference stays the same for each Student object. Since the value of these fields changes in the main-method of the converter, they will also change in every student object. That is of course if he just sets them directly in the Student-class. –  Jul 10 '13 at 22:31
  • No they're not. Java is **pass by value**, so the constructor of `Student` will receive different values on each call (even if the parameters are declared `static` in the client class). The problem looks to be `static` fields in the `Student` class, not in `Converter` class. – Luiggi Mendoza Jul 10 '13 at 22:32
  • It's actually call-by-reference in Java if an object is being passed. –  Jul 10 '13 at 22:33
  • What do you mean by *call by reference*? – Luiggi Mendoza Jul 10 '13 at 22:33
  • If you pass an object to a method, that same exact variable is passed as in the exact same pointer to a certain memory location that contains the variable value. But again, this is only true for instances that extend the Object class. –  Jul 10 '13 at 22:35
  • Thank you guys for helping , I tried both the suggestions and YES the static fields in my Students class cause the problem. , I cant believe i didnt figure that out , I'm such a slow learner , thank you for help , – misserandety Jul 10 '13 at 22:37
  • That's what I thought you meant. You're wrong. Refer to a further explanation on the subject: http://stackoverflow.com/q/40480/1065197. Also in this case OP's sending primitive types (`int` and `char`) and `String` that is immutable so it will pass a copy of the object. – Luiggi Mendoza Jul 10 '13 at 22:37