不停止输出

时间:2011-10-16 13:05:22

标签: java loops

我正在运行搜索同义词的代码。不幸的是,输出返回不间断循环,如下所示。

Synonym 0: pupil

Synonym 0: student

Synonym 0: pupil

Synonym 0: student

Synonym 0: pupil

Synonym 0: student

Synonym 0: pupil

Synonym 0: student

Synonym 0: pupil
.
.
.

我有没有想念。我试图存储在临时变量中检索的同义词来阻止无限循环。但是,它不起作用。任何人都可以帮助我吗?

public class GUIsynonym extends JFrame implements ActionListener
{

    public void displaySynonym()
    {   

        //call wordnet library  
        RiWordnet wordnet = new RiWordnet();

        String word = lineTF.getText();

        //new search 
        Vector<String> newVec = new Vector<String>();
        String[] synset = wordnet.getAllSynsets(word, "n");
        for (int k=0; k<synset.length; k++)
        {
            newVec.add(synset[k]);

            System.out.println(newVec.toArray());

            synset[k] = new String();
            for(int i=0; i<newVec.size(); i++)
            {
                int cP = newVec.indexOf(newVec.get(i));
                if(cP !=i)
                {

                    newVec.remove(cP);

                }
            }
            printSynset(word);
        }


        printSynset(word);


        String outputSynset = "Word: " + word;


        GUIsynonymTA.append("\n");
        GUIsynonymTA.append(outputSynset);
        GUIsynonymTA.append("\n");


    }// end displaySynonym()

    public static String printSynset(String word)
    {
        //call wordnet library  
        RiWordnet wordnet = new RiWordnet();
        //call stemmer method
        PorterStemmer s = new PorterStemmer();


        Vector<String> synsetVec = new Vector<String>();
        String[] synset = wordnet.getAllSynsets(word, "n");

        for (int k=0; k<synset.length; k++)
        {
            synsetVec.add(synset[k]);

            if (!synsetVec.isEmpty())
            {
                for (int j = 0; j < synsetVec.size(); j++) 
                {
                    LinkedHashSet<String> unique = new LinkedHashSet<String>();
                    LinkedHashSet<String> dups = new LinkedHashSet<String>();
                    Vector<String> newVec = new Vector<String>();   
                    for (String a : synsetVec)
                        if (!unique.add(a))
                        {
                           dups.add(a);
                           System.out.println("Duplicate detected: " + a);

                        }
                        else
                        {
                            Iterator<String> it = unique.iterator();

                            while(it.hasNext())
                            {

                            unique.removeAll(dups);

                            GUIsynonymTA.append("\n");
                            GUIsynonymTA.append(" Synonym " + j + ": " + it.next());
                            GUIsynonymTA.append("\n");

                            newVec.add(a);
                            unique.addAll(newVec);
                            unique.removeAll(newVec);
                            printSynset(synsetVec.get(j));
                            }
                        }


                }
            }

            else 
                GUIsynonymTA.append("No synsets");
                //if (synsetVec.isEmpty())
                //return word;
        }
        return word;
    }//end printSynset()

谢谢。

1 个答案:

答案 0 :(得分:0)

修改

我编辑了您的问题,使用<pre>在本网站上无法使用,请使用{ }按钮格式化代码:-)这误导我们显示无效的源代码,因此有关您的-loops。

似乎你在通话中进入无限递归;

                    printSynset(synsetVec.get(j));

此外,您在使用它的迭代器时对unique集合进行了更改。这将导致未定义的行为。