how to stop index of loop

Page 1 of 1

5 Replies - 198 Views - Last Post: 23 January 2013 - 08:40 PM Rate Topic: -----

#1 madjass  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 37
  • Joined: 28-September 12

how to stop index of loop

Posted 23 January 2013 - 05:11 PM

i want to count number of times "dog" appears on a sentence but my code its creating a loop

how can i stop this loop??


println(input);
    int index = -1;
    int count = 0;
    print("Counting dogs:");
    do{
       index = input.indexof("dog"); // the loop starts here
      if(index != -1){
        count++;
        print(count+" ");
      }
    }while(index != -1);
    println("The word \"dog\" appears "+count+" times.");
  } 
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to stop index of loop

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to stop index of loop

Posted 23 January 2013 - 05:17 PM

the standard way to exit a loop in Java C/C++ is the break statement

really really often people play (and try to make to work) loop using do while or juist while
Most of the time the easiest way is

while(true) {
   if(condition)
      break;
   ...
}


Was This Post Helpful? 0
  • +
  • -

#3 madjass  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 37
  • Joined: 28-September 12

Re: how to stop index of loop

Posted 23 January 2013 - 05:24 PM

View Postpbl, on 23 January 2013 - 05:17 PM, said:

the standard way to exit a loop in Java C/C++ is the break statement

really really often people play (and try to make to work) loop using do while or juist while
Most of the time the easiest way is

while(true) {
   if(condition)
      break;
   ...
}



do you have any idea how i would implement it because it didnt work
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,874
  • Joined: 12-December 12

Re: how to stop index of loop

Posted 23 January 2013 - 05:39 PM

Quote

int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.


The reason your original code doesn't work is that you are constantly looking for 'dog' from the beginning of the text - and it will continue to find the first occurrence of this word, over and over. You need to use the second argument (fromIndex) to start after the word that you have just found.

Start by searching from the beginning of the text:
int index = -1; // this will work okay, if you..

index = input.indexof("dog", index + 1); // the loop starts here


That is, continuing looking after the letter 'd' of 'dog'.

Ideally you should add the length of the word 'dog' to index, so that it will continue looking after the entire word 'dog'.

This post has been edited by andrewsw: 23 January 2013 - 05:40 PM

Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: how to stop index of loop

Posted 23 January 2013 - 05:55 PM

Please don't create duplicate topics. I've already told you why this creates an infinite loop in your earlier thread.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: how to stop index of loop

Posted 23 January 2013 - 08:40 PM

Closing the thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1