计算句子中重复单词的总数

我想计算句子中重复单词或重复单词的总数。在这里我可以打印单词但无法计算这些单词。


import java.util.*;

public class Duplicate {  

    

    public static void main(String[] args) {  

       

        String input = "Big black bug bit a big black dog on his big black nose";  

        //Scanner scanner = new Scanner(System.in);

         //System.out.println("Enter the sentence");

        

        //String input = scanner.nextLine();

        

        int count; 

        int counter=0;

         

        //Converts the string into lowercase  

        input = input.toLowerCase();  

          

        //Split the string into words using built-in function  

        String words[] = input.split(" ");  

          

        System.out.println("Duplicate words in a given string : ");   

        for(int i = 0; i < words.length; i++) {  

            count = 1;  

            for(int j = i+1; j < words.length; j++) {  

                if(words[i].equals(words[j])) {  

                    count++;  

                    //Set words[j] to 0 to avoid printing visited word  

                    words[j] = "0";  

                    counter=counter+1;

                    

                }  

            }  

              

            //Displays the duplicate word if count is greater than 1  

            if(count > 1 && words[i] != "0")  {

                System.out.println(words[i]);

            }

            

        } 

         

        System.out.println("Total Duplicate words in a given string : " +counter);

}  

}

我期望输出:--


给定字符串中的重复单词:big black


给定字符串中的重复单词总数:2


输出如下:


给定字符串中的重复单词:big black


给定字符串中的重复单词总数:10


总计数显示 10,而不是 2。


aluckdog
浏览 140回答 2
2回答

守着一只汪

将增量移动到counter此处应该可以:if (count > 1 && !words[i].equals("0")) {    counter++;    System.out.println(words[i]);}而不是在第二个循环中。counter每次您发现另一个重复项时,现在都会增加。counter仅当您发现存在重复单词时,向下移动才会增加它。也就是说,可以通过使用Map来计算每个单词出现的次数来简化此方法。

波斯汪

尝试使用 1 个HashMap而不是 2 个 for 循环,如下所示public static void main(String[] args) {&nbsp;&nbsp;&nbsp; &nbsp; String input = "Big black bug bit a big black dog on his big black nose";&nbsp;&nbsp;&nbsp; &nbsp; HashMap<String, Integer> dupCount = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp;//Converts the string into lowercase&nbsp;&nbsp;&nbsp; &nbsp; input = input.toLowerCase();&nbsp;&nbsp;&nbsp; &nbsp; //Split the string into words using built-in function&nbsp;&nbsp;&nbsp; &nbsp; String words[] = input.split(" ");&nbsp;&nbsp;&nbsp; &nbsp; System.out.println("Duplicate words in a given string : ");&nbsp; &nbsp;&nbsp; &nbsp; for(int i = 0; i < words.length; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(dupCount.containsKey(words[i])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int cnt = dupCount.get(words[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt = cnt + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dupCount.put(words[i], cnt);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dupCount.put(words[i], 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for(Map.Entry<String, Integer> test : dupCount.entrySet()){&nbsp; &nbsp; &nbsp; &nbsp; if(test.getValue() > 1)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duplicate words in a given string : " +test.getKey() + " : " + test.getValue());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在这种情况下,每次出现重复时都会打印最后一条语句,您可以根据需要对其进行修改。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java