tags:

views:

1032

answers:

9

Why is the following algorithm not halting for me? (str is the string I am searching in, findStr is the string I am trying to find)

    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int lastIndex = 0;
    int count =0;

    while(lastIndex != -1){

           lastIndex = str.indexOf(findStr,lastIndex);

           if( lastIndex != -1){
                 count ++;
          }
        lastIndex+=findStr.length();
    }
    System.out.println(count);

Thanks!

EDIT- updated, still not working

+3  A: 

Increment lastIndex whenever you look for next occurence. Otherwise it's always finding the first substring (at position 0).

Stanislav Kniazev
+2  A: 

public int indexOf(int ch, int fromIndex)

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

So your lastindex value is always 0 and it always finds hello in the string.

Bhushan
+1  A: 

try adding lastIndex+=findStr.length() to the end of your loop, otherwise you will end up in an endless loop because once you found the substring, you are trying to find it again and again from the same last position.

Thorsten
I did this and the function still does not work.
+2  A: 
    String str = "helloslkhellodjladfjhello";
    String findStr = "hello";
    int lastIndex = 0;
    int count = 0;

    while ((lastIndex = str.indexOf(findStr, lastIndex)) != -1) {
        count++;
        lastIndex += findStr.length() - 1;
    }

    System.out.println(count);

at the end of the loop count is 3; hope it helps

dfa
+2  A: 

do you really have to handle the matching yourself ? especially if all you need is the number of occurences, regular expressions are tidier :

    String str = "helloslkhellodjladfjhello";
    Pattern p = Pattern.compile("hello");
    Matcher m = p.matcher(str);
    int count = 0;
    while (m.find()){
     count +=1;
    }
    System.out.println(count);
Jean
+4  A: 

You "lastIndex += findStr.length();" was placed outside the brackets, causing an infinite loop (when no occurence was found, lastIndex was always to findStr.length().

Here is the fixed version :

        String str = "helloslkhellodjladfjhello";
     String findStr = "hello";
     int lastIndex = 0;
     int count = 0;

     while (lastIndex != -1) {

      lastIndex = str.indexOf(findStr, lastIndex);

      if (lastIndex != -1) {
       count++;
       lastIndex += findStr.length();
      }
     }
     System.out.println(count);
Olivier
+1  A: 

The last line in was creating a problem... last endex would never be left at -1 so there would be an infinite loop... you can fix it by moving the code in the if block.

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count =0;

while(lastIndex != -1){

       lastIndex = str.indexOf(findStr,lastIndex);

       if( lastIndex != -1){
             count ++;
             lastIndex+=findStr.length();
      }
}
System.out.println(count);
codebreach
This reply is the exact copy of the post I made an hour before ;)
Olivier
Note that this might or might not return the result expected. With substring "aa" and string to search "aaa" the number of occurences expected may be one (returned by this code), but may be two as well (in this case you'll need "lastIndex++" instead of "lastIndex += findStr.length()") depending on what you are looking for.
Stanislav Kniazev
@olivier didnt see that... :(@stan thats absolutely correct... i was just fixing the code in the problem... guess it depends on what bobcom means by number of occurrences in the string...
codebreach
do{...} while(lastIndex != -1)would be a better fit, esp. when processing multiple lines.
alex
A: 

A shorter version. ;)

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(str.split(findStr).length-1);
Peter Lawrey
A: 

How about using StringUtils.countMatches from Apache Commons Lang?

String str = "helloslkhellodjladfjhello";
String findStr = "hello";

System.out.println(StringUtils.countMatches(str, findStr));

That outputs:

3
A_M