Retrieve all the students in the same year(Java ArrayList)?

Retrieve all the students in the same year(Java ArrayList)?

FoundStudents.jsp year- parameter I receive from a form search- is an object of type Student, with ID, name, year

<%
Student search = null;
ArrayList<Student>found_list = new ArrayList<Student>();

 if((request.getParameter("year") != null)||(request.getParameter("name") != null)){

    //reading params from the form
        ..............................................
       String year_prime = request.getParameter("year");
       int year = 0;
       try{
         year = Integer.parseInt(year_prime);
        }catch(Exception e1){
        year = 0; 
       }

       if(year > 0){
             int index = 0;
         search = StudentsManager.getInstance().studByYear(year,index);

          if(search != null){//if found 1 student by year

          do{//Search all students in the same year
               search = null;
               search = StudentsManager.getInstance().studByYear(year, index);

               if(search != null){
                  found_list.add(search);
                  index++;
               }
               else {
                     index++;
                    }
            }while(search != null);
          }

      if(found_list != null && !found_list.isEmpty()){
%>
<html>
  "Displaying all the students in the "year" year with ID, name and year"
</html>


StudentsManager.java

public Student studByYear(int value, int index){

    ArrayList<Student> students = StudentsManager.getInstance().getStudents();

    for (int i = index; i < students.size(); i++) {

        if(students.get(i).getYear() == value){
           return students.get(i);
        }
        else {
            return null;
             }
    }                                       
return null;
}

students- contains all the students in the DB (I used it in another method and it worked)

I used 2 parameters for this method: value = the year, index = search the ArrayList students starting from this index, not from the beginning everytime

View Answers









Related Tutorials/Questions & Answers:

Ads