2

I am iterating over a vector of string as:

for(String s : st){
    while(s.equals("a")) //just an example, not exactly this required
    {
        //go to next element : How to do this?
    }
    System.out.println(s);
}

How to iterate over next elements within the for(:) loop?

EDIT:

As many asked the logic of while,

the vector of string is basically containing single words of a sentence and I have to collapse Noun Phrases in the sentence, e.g. if there is a sentence like "Robert Sigwick is going home". so right now st[0] has "Robert" and st[1] has "Sigwick". After the processing I have to make st[0] = "Robert Sigwick".

So my code is somewhat like:

for(String s : st){
        string newEntry = "";
        while(getPOS(s).equals("NNP")) 
        {
            newEntry += s;
            // HERE I WANT THE HELP : something like s = getNext();
        }
        if(!newEntry.equals(""))
            result.add(newEntry);
    }
user3392464
  • 141
  • 1
  • 9
  • You need to provide clarification on your question. People are making a lot of assumptions right now. – Cruncher Mar 10 '14 at 19:56
  • I saw those responses, and so I have added the explanation few minutes before. Thanks!! – user3392464 Mar 11 '14 at 08:48
  • I still don't understand why you need an inner while loop... what is `getPos(s)`? You've explained the problem, but I think you need to explain the code you've written so far. The answer to your current question is no, you can't. There's a contract with a for loop that it will iterate all entries, you can't mess with the iterator that it's using outside of regular loop constructs. – Cruncher Mar 11 '14 at 13:05
  • getPOS(s) is a function to get Part of Speech(like NNP - noun phrase etc.) for that word. See a demo at [link](http://cogcomp.cs.illinois.edu/demo/pos/?id=4) Anyhow, the question was put as I wondered whether there is a way for it, and you answered that "There's a contract with a for loop that it will iterate all entries". Thanks!! I got my work done by conventional for loop, and doing `i++` inside the while loop. – user3392464 Mar 11 '14 at 14:59

3 Answers3

0

Use loop labels with continue

OUTER:
for(String s : st){
    while(s.equals("a")) //just an example, not exactly this required
    {
        //go to next element : How to do this?
        continue OUTER;
    }
    System.out.println(s);
}

NOTE: The loop label is only necessary if you have a nested loop. If the while should be an if-statement then a simple continue; will work.

also if it's an if-statement there could be even better ways. Consider:

for(String s : st){
    if(!s.equals("a")) //just an example, not exactly this required
    {
        System.out.println(s);
    }
}

The problem here is that the whole method goes one level deeper. It's a preference thing.

More on loop labels: "loop:" in Java code. What is this, why does it compile?

Community
  • 1
  • 1
Cruncher
  • 7,641
  • 1
  • 31
  • 65
0
for(String s : st){
    if(s.equals("a")) //just an example, not exactly this required
    {
        //go to next element : How to do this?
       continue;
    }
    System.out.println(s);
}
rpax
  • 4,468
  • 7
  • 33
  • 57
0

You need to explain why you need a while loop. You cannot go to next element this way.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186