English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've written a code in while loop that finds all occurences of a string in a text. Works great, but now I want to stop the loop at the end, because on each cycle it finds a given string and highlites it in the text. So I want it to stop so that user sees every instance and let him continue by pressing the key. Im sure it can be done, but don't know how.

2007-12-30 15:08:22 · 0 answers · asked by CooK 2 in Computers & Internet Programming & Design

quote:"When he stops pressing the key"
I'm interested in him beginnig oressing the key. The point is: The code works exactly as I want, except that it does all in loops. Every cycle of the loop finds the word user has entered, highlights it, then goes to the begginning, find another ocurence if there is one, highlights it (but of course, dehighlight the previous one) and so on until the last ocurence is found. Then the loop exits leaving only the last ocurence highlighted. I want to put a statement in a program to pause the loop after first cycle, as then it highlights the first ocurence, and then to wait for user to press a key if he wants to see another match, and so on. break and continue are of no use here as they exit the loop, which I don't want. I have already achieved proper exit from my loop

2007-12-30 16:09:45 · update #1

0 answers

You set a boolean to true, and add it to the while loop conditions with AND.

When he stops pressing the key, you change the boolean to false, which ends the while loop.

2007-12-30 15:18:56 · answer #1 · answered by Anonymous · 0 0

Java Exit For Loop

2016-10-30 23:07:41 · answer #2 · answered by vides 4 · 0 0

First you need a method that just finds the first occurrence of a string from position n.
If you have a previousPosition > -1 then remove the highlight for the previous text.
Find the next occurrence
If findPosition > -1
previousPosition = findPosition
n = findPosition+1

Note: There is no loop.

Now create a key listener to call your new method when the correct key is pressed.

2007-12-31 00:33:52 · answer #3 · answered by AnalProgrammer 7 · 0 0

The BREAK statement exits a while loop in javascript.

Here's a bit of code I stole from devguru.com

var i = 0
while (i < 10)
{
document.write(i);
if (i==7)
{
document.write("the counter has reached " + i);
break;
}
i++;
}

If that's not enough to help you out, post your relevant code so we can see exactly what you're trying to do.

2007-12-30 15:22:53 · answer #4 · answered by rod 6 · 1 0

you want to highlight each occurrence of the string as it appears? or do you want to highlight all of them, then let the user continue whatever he/she was doing?

for the 1st scenario:

while(true){
if(MATCH_FOUND)
//ask the user what he wants to do
if(USER_WANTS_TO_CONTINUE)
continue; //will start loop at the beginning
else
break; //will exit the loop

for the 2nd scenario:

while(THERE_ARE_MATCHES) { };

2007-12-30 15:24:37 · answer #5 · answered by BG 2 · 0 0

fedest.com, questions and answers