java - How can I fix output from ArrayList? -


i'm writing program reads file. each line in file contains information student.each student represented object class "student". class student has method getname returns student's name.the method goes through file returns , arraylist containing student objects. problem every time use loop access arraylist , name of each student, name of last student in list. method go through file called "fileanalyzer" below code.

import java.io.*; import java.util.arraylist; import java.util.list;  public class studentstats {  public static void main(string[] args) {      list<student> example = null;     example = fileanalyzer("c:\\users\\achraf\\desktop\\ach.csv");      ( int = 0; < example.size(); i++)     {         system.out.println(example.get(i).getname());     }  }  public static list<student> fileanalyzer(string path) //path path file {      bufferedreader br = null;     list<student> info = new arraylist<student>();     string line = "";      try {         br = new bufferedreader (new filereader(path));          while ((line = br.readline()) != null)         {             //we create object "student" , add list              info.add(new student(line));          }          }      catch (filenotfoundexception e) {         system.out.println("aucun fichier trouvé");     } catch (ioexception e) {         e.printstacktrace();     }      {         if (br != null) {             try {                 br.close();             } catch (ioexception e) {                 e.printstacktrace();             }         }      }      return info; } 

in case need it, here's code class student

// class create objects each student  public class student {      private static string name ="";     private static string sex = "";     private static string grade = "";      //constructor     public student(string infos)     {         string [] etudiant = infos.split(",");          name = etudiant[0];         sex = etudiant[1];         grade = etudiant[2];                 }      // getter functions      public string getname()     {         return name;     }     public string getsex()     {         return sex;     }     public string getgrade()     {         return grade;     }  } 

below content of typical file programs reads.

lovett,m,12 achos,f,23 loba,m,24 

the real problem after running code names, name "loba" 3 times instead of getting names.

here problem in student class:

private static string name =""; private static string sex = ""; private static string grade = ""; 

you need remove static member variables otherwise objects share same attributes , hence see last values written in variables always.

learn more instance , class variables here: http://docs.oracle.com/javase/tutorial/java/javaoo/classvars.html


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -