user6308605 user6308605 - 6 months ago 9
Java Question

Replace or hide similar values in servlet

Problem:

I am retrieving value from database where it will match the the

availabilityID
of an supervisor and examiner. However, if there is no
availabilityID
match between these two, the
availabilityID
will be 0 by default.

I have made a condition in my servlet where if both
availabilityID
is 0, I want to replace it with any number chosen by my query.

part of my servlet:

for (int i=0 ; i<studentID.length ; i++){

FreeBean free = new FreeBean();

free = GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i]);

nt SVavailableID = free.getAvailableID();
int EXavailableID = free.getAvailableID();
Date FreeDate = free.getFreeDate();


out.println("<td>"+ studentID[i]+"</td>");
out.println("<td> Hello </td>");
out.println("<td>"+ supervisorID[i] +"</td>");
out.println("<td>"+ examinerID[i] +"</td>");
out.println("<td>"+ SVavailableID+"</td>");
out.println("<td>"+ EXavailableID+"</td>");
out.println("<td>"+ FreeDate+"</td>");
out.println("</tr>");

if (SVavailableID ==0 && EXavailableID ==0 )
{

free= GenerateScheduleDAO.getOtherFree(supervisorID[i],examinerID[i]);

int otherSVavailableID = free.getAvailableID();
int otherEXavailableID = free.getAvailableID();

out.println("<tr>");
out.println("<td>"+ studentID[i]+"</td>");
out.println("<td> Hello </td>");
out.println("<td>"+ supervisorID[i] +"</td>");
out.println("<td>"+ examinerID[i] +"</td>");
out.println("<td>"+ otherSVavailableID+"</td>");
out.println("<td>"+ otherEXavailableID+"</td>");
out.println("<td>"+ FreeDate+"</td>");
out.println("</tr>");

}

}// student loop


I am aware my result turns out to be like this :

Student Name | Project Title | Supervisor Name | Examiner Name | Presentation 1 | Presentation 2 | Date

abc 1111 tan tin 0 0 1/1/16
abc 1111 tan tin 12 12 1/1/16


The first row shows, the
availabilityID
for both supervisor and examiner are 0. Thus, it will execute the IF statement and find another possible
availabilityID
for them. As a result, it will duplicate the row where one consist of 0 and one consist of new value.

How can I REPLACE the row with both 0 with new value. Or AT LEAST, hide the row with both
availabilityID
=0 and only display with the new value retrieved. Is there any efficient way?

Answer

I think all you need to do is to make certain that you execute one and only one flow of logic. Using an if statement in the right place will let you achieve that.

for (int i=0; i < studentID.length; i++) {
    FreeBean free = new FreeBean();
    free = GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i]);

    int SVavailableID = free.getAvailableID();
    int EXavailableID = free.getAvailableID();
    Date FreeDate = free.getFreeDate();

    // handle the edge case here
    if (SVavailableID == 0 && EXavailableID == 0) {
        free = GenerateScheduleDAO.getOtherFree(supervisorID[i],examinerID[i]);

        SVavailableID = free.getAvailableID();
        EXavailableID = free.getAvailableID();
    }

    // now print out each record only one time
    out.println("<tr>");
    out.println("<td>"+ studentID[i]+"</td>");
    out.println("<td> Hello </td>");
    out.println("<td>"+ supervisorID[i] +"</td>");
    out.println("<td>"+ examinerID[i] +"</td>");
    out.println("<td>"+ SVavailableID+"</td>");
    out.println("<td>"+ EXavailableID+"</td>");
    out.println("<td>"+ FreeDate+"</td>");
    out.println("</tr>");
}

One other way to approach your problem would be to let the SQL query handle the double zero value for you.