【问题标题】:Searching a string arrays with an array用数组搜索字符串数组
【发布时间】:2012-07-08 05:09:04
【问题描述】:

我正在尝试使用嵌套循环在另一个用户输入的搜索词数组中搜索用户输入的文本数组,然后输出搜索词以及它们在文本中出现的次数以及总文本的百分比。我认为我走在正确的轨道上,我的问题是每次 if 语句为真时计数器都不会重置。我对编程很陌生——所以我可能完全错了。下面是整个程序。如果有人可以看看并帮我弄清楚我的问题是什么,我将永远感激不尽。

public class termFrequency {
    public static void main(String[] args) {

        String searchTextPeriod, searchTextComma, searchTextApostrophe, searchTextColon, searchTextExclamation, 
        searchTextQuestion, searchText, searchTerm;
        int counter=0, total, searchIndex=0, termIndex=0;
        double percentage=0.0;
        String [] searchArray, termArray;


        searchText = JOptionPane.showInputDialog("Enter a sentence that is at least 20 words long");

        //removes some common punctuation from the searchable text
        searchTextPeriod = searchText.replace(".", "");
        searchTextComma = searchTextPeriod.replace(",", "");
        searchTextApostrophe = searchTextComma.replace("'", " ");
        searchTextColon = searchTextApostrophe.replace(":", " ");
        searchTextExclamation = searchTextColon.replace("!", "");
        searchTextQuestion = searchTextExclamation.replace("?", "");

        searchArray = searchTextQuestion.split(" "); //splits the sentence and and puts it into an array

        total=searchArray.length;
        System.out.println("There are " +total +" words in your sentence");

        searchTerm = JOptionPane.showInputDialog("Enter your search terms here seperated by a space");
        termArray = searchTerm.split(" ");
        DecimalFormat two = new DecimalFormat("#0.00");

        boolean found = false;
        for (termIndex=0; termIndex<termArray.length; termIndex++) 
        {
            for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

                if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
                {
                    counter++; 
                    found = true;

                    percentage= ((double) counter/(double)total) * 100;
                }
                if (found) 
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
                else  
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");

            }
        }
    }
}

【问题讨论】:

    标签: java arrays loops counter


    【解决方案1】:

    您必须将“found”上的 if/else 从内部循环移动到第一个循环的末尾。 您还需要在第一个循环中重置布尔值和计数器,就像您使用初始值开始分析 termArray 中的每个新单词一样。

       for (termIndex=0; termIndex<termArray.length; termIndex++) 
        {
            counter=0; //Reset the counter for each word in termArray
            found=false; //Reset the "found" flag for each word in termArray
            for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 
    
                if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex]))
                {
                    counter++;
                    percentage= ((double) counter/(double)total) * 100;
    
                    found=true
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
                }
            }
            if (found) 
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement." );
            else  
                    System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement.");
    
        }
    

    顺便说一句,您实际上并不需要“找到”变量,现在如果 counter == 0 您知道在 searchArray 中没有找到该词。

    【讨论】:

    • 非常感谢!抱歉延迟回复!
    【解决方案2】:

    Move found = false 在第一个循环内。这样,每次迭代都会将其重置为 false。现在,如果它曾经更改为 true,那么它在整个过程的其余部分都保持为 true。

    【讨论】:

    • 非常感谢!抱歉延迟回复!
    • 没问题!我的回答没有上面那个人那么有说服力,所以几乎不需要感谢!祝你编码顺利!
    猜你喜欢
    • 2014-10-06
    • 2012-01-13
    • 2022-01-08
    • 1970-01-01
    • 2023-03-13
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多