java - break command does not stop the loop -


hello everyone, have code first noun of sentence parsed result. wrote following code. there seems problem. if statement not break loop. can 1 please me fix it?

thanks in advance.

public static string find_noun(parse p) {       label:      for(parse k:p.getchildren())     {            if((k.gettype()).equals("nn")||(k.gettype()).equals("nnp")||            (k.gettype()).equals("nns")||(k.gettype()).equals("nnps"))         {             noun=k.tostring();                           system.out.println(noun);             break label;            // aware label not needed,                                     // doesn't work either way.         }         else         {             system.out.println("else  "+k);             find_noun(k);         }     }     return noun; } 

input:

became\vbd a\dt regular\jj customer\nn of\in a\dt suburban\jj garden\nn

the output is:

else  became else  became else  regular customer of suburban garden else  regular customer else  else  else  regular else  regular customer \\this string extracted else  of suburban garden else  of else  of else  suburban garden else  else  else  suburban else  suburban garden garden  

the problem you're calling find_noun recursively on every non-noun. yes, is breaking out of loop 1 iteration you're looking at... it's getting previous level of stack.

it's not clear me why you're recursing @ all, if do need recurse, need way of detecting whether recursive call found noun or not, , returning if did. possibly this:

// renamed method follow java naming conventions public static string findnoun(parse p) {         for(parse k : p.getchildren())     {            // removed bunch of extraneous brackets, , added whitespace         // readability. should consider set noun_types         // use if (noun_types.contains(k.gettype()) instead.         if (k.gettype().equals("nn") || k.gettype().equals("nnp") ||             k.gettype().equals("nns")|| k.gettype().equals("nnps"))         {             return k.tostring();         }         else         {             string possiblenoun = findnoun(k);             // return if recursion found noun             if (possiblenoun != null)             {                 return possiblenoun;             }         }     }     // nothing found in node or children: tell caller     return null; } 

Comments