title: Need data from txt file, comma delimited, to populate an arraylist with objects from existing class
body: <p>I need to:</p>
  
  <ul>
  <li>take the input from a text file and read it line by line, </li>
  <li>take each line and split it into student objects and send those values to an arraylist,</li>
  <li>send the arraylist of objects from the Student class to my servlet, </li>
  <li>send the arraylist from the servlet on to my jsp page </li>
  <li>Display the values in an html table.</li>
  </ul>
  
  <p>Here are my pages for this particular function. I have a number of different java pages, servlets, and jsp pages involved in this project so if you need anything from those please just ask.</p>
  
  <p>The text file contains <code>Student</code> information in the following order: </p>
  
  <blockquote>
    <p>Student ID, Last Name, First Name, Quiz Scores 1-5, Makeup Quiz Score,
    Midterm Score, Problems Score, and Final Exam Score.</p>
  </blockquote>
  
  <p>and an example of the format of the text file is as follows:</p>
  
  <blockquote>
    <p>1234,Some,Person,98.0,94.0,97.0,96.0,99.0,0.0,92.0,95.0,99.0
    1235,Another,Person,89.0,99.0,87.0,85.0,88.0,0.0,89.0,98.0,99.0
    1236,Onemore,Person,89.0,98.0,89.0,98.0,97.0,0.0,79.0,89.0,99.0</p>
  </blockquote>
  
  <p>The output I am getting in the table however is the first line only and its all in the first cell.</p>
  
  <p>If you could point me in the right direction I am completely lost. </p>
  
  <p>Here is my <code>Student</code> class page:</p>
  
  <pre><code>public class Student {
  private String sid,fnm,lnm,lgrade;
  private double q1,q2,q3,q4,q5,qmkup,midt,probs,finex,qavg,cavg;
  
  public Student (){
      //empty constructor
      sid="";
      lnm="";
      fnm="";
      lgrade="";
      q1= 0;
      q2= 0;
      q3= 0;
      q4= 0;
      q5= 0;
      qmkup= 0;
      midt= 0;
      probs= 0;
      finex= 0;
      qavg= 0;
      cavg= 0;
  }
  
  
  
  public String getSid() {
      return sid;
  }
  
  public void setSid(String sid) {
      this.sid = sid;
  }
  
  public String getFnm() {
      return fnm;
  }
  
  public void setFnm(String fnm) {
      this.fnm = fnm;
  }
  
  public String getLnm() {
      return lnm;
  }
  
  public void setLnm(String lnm) {
      this.lnm = lnm;
  }
  
  public String getLgrade() {
      return lgrade;
  }
  
  public double getQ1() {
      return q1;
  }
  
  public void setQ1(double q1) {
      this.q1 = q1;
  }
  
  public double getQ2() {
      return q2;
  }
  
  public void setQ2(double q2) {
      this.q2 = q2;
  }
  
  public double getQ3() {
      return q3;
  }
  
  public void setQ3(double q3) {
      this.q3 = q3;
  }
  
  public double getQ4() {
      return q4;
  }
  
  public void setQ4(double q4) {
      this.q4 = q4;
  }
  
  public double getQ5() {
      return q5;
  }
  
  public void setQ5(double q5) {
      this.q5 = q5;
  }
  
  public double getQmkup() {
      return qmkup;
  }
  
  public void setQmkup(double qmkup) {
      this.qmkup = qmkup;
  }
  
  public double getMidt() {
      return midt;
  }
  
  public void setMidt(double midt) {
      this.midt = midt;
  }
  
  public double getProbs() {
      return probs;
  }
  
  public void setProbs(double probs) {
      this.probs = probs;
  }
  
  public double getFinex() {
      return finex;
  }
  
  public void setFinex(double finex) {
      this.finex = finex;
  }
  
  public double getQavg() {
      calcStudent();
      return qavg;
  }
  
  public double getCavg() {
      return cavg;
  }
  
  private void calcStudent(){
      double[] qs = { q1, q2, q3, q4, q5, qmkup};
      Arrays.sort(qs);
      qavg = (qs[2] + qs[3] + qs [4] + qs[5]) / 4.0;
  
      if (qavg &gt;= 89.5 &amp;&amp; midt &gt;= 89.5 &amp;&amp; probs &gt;= 89.5){
          cavg = (qavg+midt+probs) / 3.0;
          lgrade = "A";
      }else{
          cavg = (qavg * .5) + (midt * .15) + (probs * .1) + (finex * .25);
          if (cavg &gt;= 89.5){
              lgrade = "A";
          }else if (cavg &gt;= 79.5){
              lgrade = "B";
          }else if (cavg &gt;= 69.5){
              lgrade = "C";
          }else if (cavg &gt;= 59.5){
              lgrade = "D";
          }else{
              lgrade = "F";
          }
      }
  }
  
  @Override
  public String toString(){
      String s = sid + "," + lnm + "," + fnm + "," + 
              q1 + "," + q2 + "," + q3 + "," + q4 + "," + q5 + "," + 
              qmkup + "," + midt + "," + probs + "," + finex;
  
      return s;
  }
  </code></pre>
  
  <p>Here is my java page:</p>
  
  <pre><code>public class StudentIO {
  public static void addStudent(Student s, String path)
               throws IOException{
      File f = new File(path);
      PrintWriter out = new PrintWriter(new FileWriter(f,true));
      out.println(s.toString());
      out.close();
  }
  
  public static ArrayList&lt;Student&gt; getStudentList(String path)
                throws IOException {
  
      BufferedReader in = new BufferedReader
              (new FileReader(path));
  
      ArrayList&lt;Student&gt; sList = new ArrayList&lt;Student&gt;();
      String line = in.readLine();
      String data[];
  
  
      while (line != null){
          data = line.split(",");
          Student s = new Student();
  
          s.setSid(data[0]);
          s.setLnm(data[1]);
          s.setFnm(data[2]);
          s.setQ1(Double.parseDouble(data[3]));
          s.setQ2(Double.parseDouble(data[4]));
          s.setQ3(Double.parseDouble(data[5]));
          s.setQ4(Double.parseDouble(data[6]));
          s.setQ5(Double.parseDouble(data[7]));
          s.setQmkup(Double.parseDouble(data[8]));
          s.setMidt(Double.parseDouble(data[9]));
          s.setProbs(Double.parseDouble(data[10]));
          s.setFinex(Double.parseDouble(data[11]));
  
          s.getSid();
          s.getLnm();
          s.getFnm();
          s.getQ1();
          s.getQ2();
          s.getQ3();
          s.getQ4();
          s.getQ5();
          s.getQmkup();
          s.getMidt();
          s.getProbs();
          s.getFinex();
  
          sList.addAll(Arrays.asList(s));
  
          line = in.readLine();
          }
      in.close(); 
      return sList;
  
  }
  </code></pre>
  
  <p>Here is my servlet:</p>
  
  <pre><code>public class ClassListServlet extends HttpServlet {
  
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      response.setContentType("text/html;charset=UTF-8");
      String emsg = "";
      String path;
      String URL = "/ClassList.jsp";
  
      ArrayList&lt;Student&gt; stulist;
  
      try{
          path = getServletContext().getRealPath("/WEB-INF/classlist.txt");
          stulist = StudentIO.getStudentList(path);
          request.setAttribute("stulist", stulist);
      }catch (Exception e){
          emsg = "Process error: " +e.getMessage();
          URL = "/students.jsp";
      }
      RequestDispatcher disp = getServletContext().getRequestDispatcher(URL);
      disp.forward(request, response);
  }
  </code></pre>
  
  <p>And here is the JSP page:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;Class List&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;h1&gt;Students on file:&lt;/h1&gt;
      &lt;table&gt;
          &lt;tr&gt;    
              &lt;th colspan="12"&gt;&lt;br&gt;Class List&lt;/th&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;th colspan="1"&gt;Student ID&lt;/th&gt;
              &lt;th colspan="1"&gt;Last Name&lt;/th&gt;
              &lt;th colspan="1"&gt;First Name&lt;/th&gt;
              &lt;th colspan="1"&gt;Quiz 1&lt;/th&gt;
              &lt;th colspan="1"&gt;Quiz 2&lt;/th&gt;
              &lt;th colspan="1"&gt;Quiz 3&lt;/th&gt;
              &lt;th colspan="1"&gt;Quiz 4&lt;/th&gt;
              &lt;th colspan="1"&gt;Quiz 5&lt;/th&gt;
              &lt;th colspan="1"&gt;Make-Up Quiz&lt;/th&gt;
              &lt;th colspan="1"&gt;Midterm&lt;/th&gt;
              &lt;th colspan="1"&gt;Problems&lt;/th&gt;
              &lt;th colspan="1"&gt;Final&lt;/th&gt;
          &lt;/tr&gt;
              &lt;%
  
                  ArrayList&lt;Student&gt; studt = (ArrayList&lt;Student&gt;) 
                                          request.getAttribute("stulist");
  
                  for (int i = 0; i &lt; studt.size(); i++) {
              %&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;%= studt.get(i) %&gt;&lt;/td&gt;
          &lt;/tr&gt;
  
  
              &lt;% } %&gt;
      &lt;/table&gt;
      &lt;% 
         //cells and titles for each output field     
         //every time through loop will process each individual
         //student object instead of string objects
      %&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>