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

A bit stuck here. I am trying to write a method: public void addAssignment(Strin

ID: 650835 • Letter: A

Question

A bit stuck here. I am trying to write a method:

public void addAssignment(String string) {
       try {
       //FileInputStream fsIP= new FileInputStream(new File("\DB\GradesDatabase.xlsx"));
       Sheet sheet = workbook.getSheet("IndividualContribs");
       int num = sheet.getRow(0).getPhysicalNumberOfCells();
       //Go to first empty slot on top row
       int firstEmpty = num+1;
       Row row = sheet.getRow(1);
       Cell column = row.getCell(firstEmpty);
       column.setCellValue(string);
       workbook.write(null);
       workbook.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

that passes the following junit test:

@Test
public void testAddAssignment() {
db.addAssignment("ASSIGNMENT: black-box testing");
db = new GradesDB(GRADES_DB);
assertEquals(4, db.getNumAssignments());
db.addAssignment("ASSIGNMENT: white-box testing");
db = new GradesDB(GRADES_DB);
assertEquals(5, db.getNumAssignments());
}

but it's not working...I think because I'm trying to call the file in my .java file, while the test file has it's own means of getting to the excel sheet:

GradesDB db = null;
static final String GRADES_DB_GOLDEN = "DB" + File.separator
+ "GradesDatabase-goldenversion.xlsx";
static final String GRADES_DB = "DB" + File.separator
+ "GradesDatabase.xlsx";

@Before
public void setUp() throws Exception {
FileSystem fs = FileSystems.getDefault();
Path dbfilegolden = fs.getPath(GRADES_DB_GOLDEN);
Path dbfile = fs.getPath(GRADES_DB);
Files.copy(dbfilegolden, dbfile, REPLACE_EXISTING);
db = new GradesDB(GRADES_DB);
}

@After
public void tearDown() throws Exception {
db = null;
}

Please help me write  public void addAssignment(String string) { correctly so that it does as this test is trying for (to insert something(s) in the first open space(s) in the next few rows in my excel DB:

Explanation / Answer

public void addAssignment(String string)

{

XSSFWorkbook db1 = new XSSFWorkbook(new FileInputStream(new File("\DB\GradesDatabase.xlsx")));

// import the file

XSSFSheet sheet = db1.getSheetAt(0);
       int num = sheet.getRow(0).getPhysicalNumberOfCells();
       //Go to first empty slot on top row
       int firstEmpty = num+1;
       Row row = sheet.getRow(1);
       Cell column = row.getCell(firstEmpty);
       column.setCellValue(string);
        db1.write(null);
        db1.close();
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();

}

Here, Apache POI is being used.

Add "import org.apache.poi.xssf.usermodel.XSSFSheet;" in the beginning.