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!

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....");
                            }
                          }
                        }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.