简体   繁体   中英

How to Make Program flow control jump back to a former loop in java?

So I have written a code that allows a user to find a word in a TextArea . I have nearly succeeded but for one thing. here, I will show you all the code and tell my problem.

if(ta.getText().length() != 0 && t1.getText().length() != 0)
{
    char c1[] = ta.getText().trim().toCharArray();
    char c2[] = t1.getText().trim().toCharArray();
    for(int i=startFlag;i<c1.length;i++)
    {
        if(c1[i]==c2[0])
        {
            start = i;
            break;
        }
    }

    k=start;
    for(int i=0;i<c2.length;i++)
    {
        if(c2[i] != c1[start++])
        {

        }
        else 
            countFlag++;
    }

    if(countFlag==c2.length)
    {
        ta.select(k,c2.length);
        startFlag++;
    }
}

For reference, ta is the TextArea and t1 is the TextField where the user enters a word to find. i have a problem in the second for loop. What should I write in the if () block there so that whenever c2[i] != c1[start++] the control is shifted to the first for loop, that would again determine the value of start?

Create a method to get "start" that you can then call whenever you want.

if(ta.getText().length() != 0 && t1.getText().length() != 0)
        {
            char c1[] = ta.getText().trim().toCharArray();
            char c2[] = t1.getText().trim().toCharArray();

            k=getStart(startFlag, c1.length);

            for(int i=0;i<c2.length;i++)
            {
                if(c2[i] != c1[start++])
                {
                    start = getStart(startFlag, c1.length);
                }
                else 
                    countFlag++;
            }

            if(countFlag==c2.length)
            {
                ta.select(k,c2.length);
                startFlag++;
            }

        }

And getStart() is:

public int getStart(int startFlag, int length) {
    for(int i=startFlag;i<length;i++)
            {
                if(c1[i]==c2[0])
                {
                    return i;
                }
            }
}

You may need different inputs to getStart(), but hopefully this gets across the general idea.

The way your code is set up right now, what you're asking for is impossible. To do what you're asking, you'll need to refactor your current code into different methods. More specifically, you'll need to put the for loops into their own methods and then call these methods.

So what you would need to do is make a separate method for the for loop.

public static int startForLoop(int i) {
    for(int i=startFlag;i<c1.length;i++)
    {
       if(c1[i]==c2[0])
       {
          start = i;
          break;
       }
    }
}

Then you can just call startForLoop(0) initially and in the 2nd for loops if statment:

if(c2[i] != c1[start++])
{
    startForLoop(start+1)
}

This will continue the for loop where it left off. If you need to run the 2nd for loop again then you have to make a separate method for it as well and basically place both of them in a while loop that continues till you find the result you want in the 2nd for loop.

May be this code piece help you what you are looking for. Basically it moves along with the string to be searched in keeping in mind the index of the string to be searched for.

Sorry but i have implemented it in java, but the notion is same and the result returned is the best what i got.you must give it a try!

private static String searchString(String searchIN,String searchFOR){
        if (searchFOR != "") {
            int index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase());
            String before = "";
            String highlighted = "";
            String after = "";
            while (index >= 0) {
                int len = searchFOR.length();
                before = searchIN.substring(0, index);
                highlighted = "\"" + searchFOR + "\"";//do what ever you want to do with searched string
                after = searchIN.substring(index + len);
                searchIN = before + highlighted + after;
                index = searchIN.toUpperCase().indexOf(searchFOR.toUpperCase(), index + highlighted.length());
            }
        }
        return searchIN;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM