Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the following code (below), which is working fine for grabbing a student

ID: 647315 • Letter: I

Question

I have the following code (below), which is working fine for grabbing a student name and id. However, I also need the getStudentByName method to return the students attendance (getAttendance()) which is located in a seperate sheet, called "Attendance", than the name and student id (as this is configured now).

Note: The students name is located in the same position on both sheets; the id is next to it on one sheet and the attendance (int) is next to it on the other. Please help me find a way to switch sheets and gather these 3 items in the listed method. Thank you.

private Student addStudent(Row row){
       Student student = new Student();
      
       Cell cell = row.getCell(0);
       if(cell.getCellType()!=Cell.CELL_TYPE_BLANK){
           String val = cell.getStringCellValue();
           student.setName(val);
       }
       cell = row.getCell(1);
       if(cell.getCellType()!=Cell.CELL_TYPE_BLANK){
           double val1 = cell.getNumericCellValue();
           student.setId(Long.toUnsignedString((long) val1));
       }  
       return student;      
   }

   public Student getStudentByName(String studentName) {
       Student student = new Student();
       Sheet sheet = workbook.getSheet("StudentsInfo");
       Iterator it = sheet.rowIterator();
       while(it.hasNext()){
           Row row = it.next();
           Cell cell = row.getCell(0);
           if(cell.getCellType()!=Cell.CELL_TYPE_BLANK){
               String val = cell.getStringCellValue();
               if(val.trim().equalsIgnoreCase(studentName)){
                   student = addStudent(row);
               }
           }
       }
       return student;
   }

Explanation / Answer

Solution:

As per the code, when you want to add the Attendance sheet and want to retrieve the attendance from the next sheet, then do the following steps.

    Step 1: In the getStudentByName(String studentName)

{

    //add the below code to the existing code:

    Sheet sheet1=workbook.getSheet("Attendance") ;

    Iterator it1= sheet1.rowIterator();

    //Since it has mentioned that both sheets contain same names with id and attendance repectively.

    while(it.hasNext() && it1.hasNext())   

    {

         Row row=it.next();

         Cell cell = row.getCell(0);

         Row row1=it1.next();

         Cell cell1=row1.getCell(0);

         if(cell.getCellType()!=Cell.CELL_TYPE_BLANK && cell1.getCellType()!=Cell.CELL_TYPE_BLANK)

         {

             String val = cell.getStringCellValue();

             if(val.trim().equalsIgnoreCase(studentName))

             {

                 student = addStudent(row, row1);

             }

         }

    }

    return student;

}

Step 2 :

//add one more parameter to the method

private Student addStudent(Row row, Row row1)

{

    Student student = new Student();

    Cell cell = row.getCell(0);

    Cell cell1=row1.getCell(0);

    if(cell.getCellType()!=Cell.CELL_TYPE_BLANK)

    {

         String val = cell.getStringCellValue();

         student.setName(val);

    }

    cell = row.getCell(1);

    cell1 =row1.getCell(1);

    if(cell.getCellType()!=Cell.CELL_TYPE_BLANK && cell1.getCellType()!=Cell.CELL_TYPE_BLANK )

    {

         double val1 = cell.getNumericCellValue();

         int attendance=cell1.getNumericCellValue();

         student.setId(Long.toUnsignedString((long) val1));

         student.setAttendance(attendance);//add a setAttendance method to the Student class so that you can set the

         //attendance to the Student

    }

    return student;     

}

Hope this code must be help full to you.