Java word count program

后端 未结 22 979
北荒
北荒 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:01

    My implementation, not using StringTokenizer:

    Map<String, Long> getWordCounts(List<String> sentences, int maxLength) {
        Map<String, Long> commonWordsInEventDescriptions = sentences
            .parallelStream()
            .map(sentence -> sentence.replace(".", ""))
            .map(string -> string.split(" "))
            .flatMap(Arrays::stream)
            .map(s -> s.toLowerCase())
            .filter(word -> word.length() >= 2 && word.length() <= maxLength)
            .collect(groupingBy(Function.identity(), counting()));
        }
    

    Then, you could call it like this, as an example:

    getWordCounts(list, 9).entrySet().stream()
                    .filter(pair -> pair.getValue() <= 3 && pair.getValue() >= 1)
                    .findFirst()
                    .orElseThrow(() -> 
        new RuntimeException("No matching word found.")).getKey();
    

    Perhaps flipping the method to return Map<Long, String> might be better.

    0 讨论(0)
  • 2020-12-09 07:01

    To count specified words only like John, John99, John_John and John's only. Change regex according to yourself and count the specified words only.

        public static int wordCount(String content) {
            int count = 0;
            String regex = "([a-zA-Z_’][0-9]*)+[\\s]*";     
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(content);
            while(matcher.find()) {
                count++;
                System.out.println(matcher.group().trim()); //If want to display the matched words
            }
            return count;
        }
    
    0 讨论(0)
  • 2020-12-09 07:02

    You can use String.split (read more here) instead of charAt, you will get good results. If you want to use charAt for some reason then try trimming the string before you count the words that way you won't have the extra space and an extra word

    0 讨论(0)
  • 2020-12-09 07:02

    This could be as simple as using split and count variable.

    public class SplitString {
    
        public static void main(String[] args) {
            int count=0;        
            String s1="Hi i love to code";
    
            for(String s:s1.split(" "))
            {
                count++;
            }
            System.out.println(count);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 07:03
    public class wordCount
    {
    public static void main(String ar[]) throws Exception
    {
    System.out.println("Simple Java Word Count Program");
    
    
        int wordCount = 1,count=1;
     BufferedReader br = new BufferedReader(new FileReader("C:/file.txt"));
                String str2 = "", str1 = "";
    
                while ((str1 = br.readLine()) != null) {
    
                        str2 += str1;
    
                }
    
    
        for (int i = 0; i < str2.length(); i++) 
        {
            if (str2.charAt(i) == ' ' && str2.charAt(i+1)!=' ') 
            {
                wordCount++;
            } 
    
    
            }
    
        System.out.println("Word count is = " +(wordCount));
    }
    

    }

    0 讨论(0)
  • 2020-12-09 07:06
    public static void main (String[] args) {
    
         System.out.println("Simple Java Word Count Program");
    
         String str1 = "Today is Holdiay Day";
    
         String[] wordArray = str1.trim().split("\\s+");
         int wordCount = wordArray.length;
    
         System.out.println("Word count is = " + wordCount);
    }
    

    The ideas is to split the string into words on any whitespace character occurring any number of times. The split function of the String class returns an array containing the words as its elements. Printing the length of the array would yield the number of words in the string.

    0 讨论(0)
提交回复
热议问题