Java word count program

后端 未结 22 981
北荒
北荒 2020-12-09 06:48

I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string

相关标签:
22条回答
  • 2020-12-09 07:16

    try this

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class wordcount {
        public static void main(String[] args) {
            String s = "India is my country. I love India";
            List<String> qw = new ArrayList<String>();
            Map<String, Integer> mmm = new HashMap<String, Integer>();
            for (String sp : s.split(" ")) {
                qw.add(sp);
            }
            for (String num : qw) {
                mmm.put(num, Collections.frequency(qw, num));
            }
            System.out.println(mmm);
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 07:17

    Use split(regex) method. The result is an array of strings that was splited by regex.

    String s = "Today is Holdiay Day";
    System.out.println("Word count is = " + s.split(" ").length);
    
    0 讨论(0)
  • 2020-12-09 07:18

    To count total words Or to count total words without repeat word count

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String test = "I am trying to make make make";
        Pattern p = Pattern.compile("\\w+");
        Matcher m = p.matcher(test);
        HashSet<String> hs =  new HashSet<>();
        int i=0;
        while (m.find()) {
            i++;
            hs.add(m.group());
        }
        System.out.println("Total words Count==" + i);
        System.out.println("Count without Repetation ==" + hs.size());
    }
    

    }

    Output :

    Total words Count==7

    Count without Repeatation ==5

    0 讨论(0)
  • 2020-12-09 07:21
        public class TotalWordsInSentence {
        public static void main(String[] args) {
    
            String str = "This is sample sentence";
            int NoOfWOrds = 1;
    
            for (int i = 0; i<str.length();i++){
                if ((str.charAt(i) == ' ') && (i!=0) && (str.charAt(i-1) != ' ')){
                    NoOfWOrds++;
                }
            }
             System.out.println("Number of Words in Sentence: " + NoOfWOrds);
        }
    }
    

    In this code, There wont be any problem regarding white-space in it.
    just the simple for loop. Hope this helps...

    0 讨论(0)
  • 2020-12-09 07:22
    import com.google.common.base.Optional;
    import com.google.common.base.Splitter;
    import com.google.common.collect.HashMultiset;
    import com.google.common.collect.ImmutableSet;
    import com.google.common.collect.Multiset;
    
    String str="Simple Java Word Count count Count Program";
    Iterable<String> words = Splitter.on(" ").trimResults().split(str);
    
    
    //google word counter       
    Multiset<String> wordsMultiset = HashMultiset.create();
    for (String string : words) {   
        wordsMultiset.add(string.toLowerCase());
    }
    
    Set<String> result = wordsMultiset.elementSet();
    for (String string : result) {
        System.out.println(string+" X "+wordsMultiset.count(string));
    }
    
    0 讨论(0)
  • 2020-12-09 07:25

    Not sure if there is a drawback, but this worked for me...

        Scanner input = new Scanner(System.in);
        String userInput = input.nextLine();
        String trimmed = userInput.trim();
        int count = 1;
    
        for (int i = 0; i < trimmed.length(); i++) {
          if ((trimmed.charAt(i) == ' ') && (trimmed.charAt(i-1) != ' ')) {
            count++;
          }
        }
    
    0 讨论(0)
提交回复
热议问题