-2

i'm having a bit of trouble getting the row number of a specific string. Let's say I have a cell content = "HC" at row 9 and using the method findRow(sheet, "HC"). I should get a return value of 9, but right now the return is 0.

The purpose of the excelSheetFinder function is after the row number is returned from findRow(), I can get the cells from a specific column that I input as the parameters(enCol and frCol).

Any help is appreciated!

public static String[] excelSheetFinder(String value, int enCol, int frCol) throws IOException{
    String[] shortNames = new String[2];
    File excelFile = new File("excel.xlsx");
    FileInputStream inputExcelFile = new FileInputStream(excelFile);
    XSSFWorkbook wb = new XSSFWorkbook(inputExcelFile);
    XSSFSheet sheet = wb.getSheetAt(0);

    int shortNameRow = findRow(sheet, value);

    System.out.println(shortNameRow);

    Row r = sheet.getRow(shortNameRow);
    String enCell = r.getCell(enCol).toString();
    String frCell = r.getCell(frCol).toString();
    shortNames[0] = enCell;
    shortNames[1] = frCell;

    return shortNames;
}

private static int findRow(XSSFSheet sheet, String value){
    //to find row number so we can search through that specific column
    int gotRow = 0;
    for (Row row: sheet){
        for (Cell cell: row){
            final DataFormatter df = new DataFormatter();
            final XSSFCell cellVal = (XSSFCell) row.getCell(row.getRowNum());
            String valueAsString = df.formatCellValue(cellVal);
            if (valueAsString.trim() == value){
                gotRow = row.getRowNum();
            }
        }
    }
    return gotRow;  
}
  • 1
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Susannah Potts Aug 17 '16 at 16:15

1 Answers1

0

Replace == by .equals for your string comparison:

  • if (valueAsString.trim() == value)

== tests for reference equality

.equals() tests for value equality

Ogma
  • 116
  • 5
  • .equals probably did fix the string comparison for future problems... But i'm still getting a return of 0(initialized var). Do you see any problems in findRow method? – user2874977 Aug 17 '16 at 16:28