user3321457
user3321457

Reputation: 15

Need data from txt file, comma delimited, to populate an arraylist with objects from existing class

I need to:

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.

The text file contains Student information in the following order:

Student ID, Last Name, First Name, Quiz Scores 1-5, Makeup Quiz Score, Midterm Score, Problems Score, and Final Exam Score.

and an example of the format of the text file is as follows:

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

The output I am getting in the table however is the first line only and its all in the first cell.

If you could point me in the right direction I am completely lost.

Here is my Student class page:

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 >= 89.5 && midt >= 89.5 && probs >= 89.5){
        cavg = (qavg+midt+probs) / 3.0;
        lgrade = "A";
    }else{
        cavg = (qavg * .5) + (midt * .15) + (probs * .1) + (finex * .25);
        if (cavg >= 89.5){
            lgrade = "A";
        }else if (cavg >= 79.5){
            lgrade = "B";
        }else if (cavg >= 69.5){
            lgrade = "C";
        }else if (cavg >= 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;
}

Here is my java page:

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<Student> getStudentList(String path)
              throws IOException {

    BufferedReader in = new BufferedReader
            (new FileReader(path));

    ArrayList<Student> sList = new ArrayList<Student>();
    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;

}

Here is my servlet:

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<Student> 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);
}

And here is the JSP page:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Class List</title>
</head>
<body>

    <h1>Students on file:</h1>
    <table>
        <tr>    
            <th colspan="12"><br>Class List</th>
        </tr>
        <tr>
            <th colspan="1">Student ID</th>
            <th colspan="1">Last Name</th>
            <th colspan="1">First Name</th>
            <th colspan="1">Quiz 1</th>
            <th colspan="1">Quiz 2</th>
            <th colspan="1">Quiz 3</th>
            <th colspan="1">Quiz 4</th>
            <th colspan="1">Quiz 5</th>
            <th colspan="1">Make-Up Quiz</th>
            <th colspan="1">Midterm</th>
            <th colspan="1">Problems</th>
            <th colspan="1">Final</th>
        </tr>
            <%

                ArrayList<Student> studt = (ArrayList<Student>) 
                                        request.getAttribute("stulist");

                for (int i = 0; i < studt.size(); i++) {
            %>
        <tr>
            <td><%= studt.get(i) %></td>
        </tr>


            <% } %>
    </table>
    <% 
       //cells and titles for each output field     
       //every time through loop will process each individual
       //student object instead of string objects
    %>
</body>

Upvotes: 1

Views: 1720

Answers (3)

Nikhil Joshi
Nikhil Joshi

Reputation: 817

When you are populating sList in StudentIO.java, does sList contains all rows from a file? Try changing while loop to:

while ( ( line = in.readLine( ) ) != null ) {

Also, you are converting Student object to a string and then sending it to your JSP. Please pass complete List object and then print it. Also, you need colspan attribute in :

<tr>
    <td><%= studt.get(i) %></td>
</tr>

Upvotes: 0

Luis Pena
Luis Pena

Reputation: 4354

You're making an instance of the ArrayList on getStudentList() Simply erase this line from that method sList = new ArrayList<Student>();

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 9946

Problem is in getStudentList() method, You do not need to create a new ArrayList every time.

sList = new ArrayList<Student>(); 

Must be moved out of while loop

Upvotes: 2

Related Questions