Find only first occurrence of any text and stop

Hi !
I have been working on searching the documents based on any query/string.
I have been able to do it and get the counts.
But, what I need is, I don’t need any counts, I just want to check if the searched query exists in the document. If it does (no matter once, or N times). I just want it stop there and search for further docs in the directory.
Following is the code that I have used. Can you please help me optimize it as per my requirement, so that I could get a better performance, in terms of speed. Here goes the code :

public static void main(String[]args) throws Exception {
File[] files = new File(“E:\docs”).listFiles();
for (File file : files) {
if (file.isFile()) {
String fileName = file.getName();
String extensionName = fileName.substring(fileName.lastIndexOf("."));
if (extensionName.equals(".xlsx") || extensionName.equals(".xls"))
{
int count = 0;
// System.out.println(“Processing document: " + fileName);
Workbook workbook = new Workbook(file.getAbsolutePath());
for(int i=0; i<workbook.getWorksheets().getCount(); i++) {
Worksheet worksheet = workbook.getWorksheets().get(i);
FindOptions opts = new FindOptions();
Cell cell = null;
do
{
cell = worksheet.getCells().find(“GmbH”, cell, opts);
if(cell!=null)
{
count++;
}
}
while(cell!=null);
}
if(count > 0)
System.out.println(“E:\”+file.getName());
// System.out.println(“E:\”+file.getName()+” || Count="+count);
}
}
}
}

Please suggest the code changes. Thanks ! :slight_smile:

@Kushal.20,

You may easily do that, see the updated code segment for your reference. You should write/update your code segment accordingly by yourself for your needs.
e.g
Sample code:


for(int i=0; i<workbook.getWorksheets().getCount(); i++) {
Worksheet worksheet = workbook.getWorksheets().get(i);
FindOptions opts = new FindOptions();
Cell cell = null;
cell = worksheet.getCells().find(“GmbH”, cell, opts);
if(cell!=null)
{
System.out.println(“string found”);
break;
}
}

Hope, this helps a bit.

@Amjad_Sahi
I actually tried , when I couldn’t get any way out, then I wrote this to you !
I tried this, but this is not solving the purpose, both approaches are taking same time. Are you sure, that this break here stops the iteration over cells, once the value is found at very first ?
Please see to it. I guess, the cell is having the complete cells in the entire sheet.

@Kushal.20,

Yes, it will be complete cells collection when you get the cells of the sheet.

Could you create two simple console application (runnable), zip the projects and attach these here to show that there is no difference (one project in which you use break to exit once it finds the first occurrence of the string and other project where you find every occurrence and gather counts). We will check your sample code and then provide our feedback. Please use a few files (10-20 which has your string at random). You may upload to some file sharing service (e.g Dropbox, Google drive, etc.) and provide Download link, so we could download it and run it.