Collectives on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more
I am currently reading values from an excel spreadsheet and assigning this value to a String variable.
Below is a sample of the code I am using:
Workbook workbook = new XSSFWorkbook(iStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
// Skips the header row
iterator.next();
// Iterate through the first sheet and get the cell values
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 0:
String userName;
if (cell.getStringCellValue() == "") {
userName = "BlankCell";
} else {
userName = cell.getStringCellValue();
break;
I have multiple blank cells within this excel spreadsheet which I am trying to replace with the phrase "BlankCell".
I have used this piece of code successfully in the past but for some reason it does not work anymore.
Is my logic in this instance wrong for replacing blank cells?
The string comparison by ==
isn't good practice, there's equals
method for this.
String userName;
if ("".equals(cell.getStringCellValue())) {
userName = "BlankCell";
} else {
userName = cell.getStringCellValue();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.