0

Hey!

Firstly, here's the code:

                        for (Row row : sheetToSearch) {
                          for (Cell cell : row) {
                            if(cell.toString().equals("Oladimeji")){
                                System.out.println("Found Oladimeji!!");
                                break;
                            } else {
                                System.out.println("Oladimeji not found....");
                            }
                          }
                        }

What this does is search through the an excel spreadsheet, looking for a name (which happens to be my surname!). It is located in the sheet once, and displays the relevant message when found. However, the loop does not exit as expected, and just keeps iterating.

Any ideas as to why?

Thanks!

2
Contributors
2
Replies
5
Views
1 Year
Discussion Span
Last Post by rubberman
0

Solved my own problem...gonna leave the solution here in case it is needed by anyone!

                    FOUND: for (Row row : sheetToSearch) {
                          for (Cell cell : row) {
                            if(cell.toString().equals("Oladimeji")){
                                System.out.println("Found Oladimeji!!");
                                break FOUND;
                                } else {
                                System.out.println("Oladimeji not found....");
                            }
                          }
                        }
0

This effectively works, but is essentially a goto - not good programming practice. Usually you would use a termination flag in the for loop, set it when found, and you are done.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.