Write your code in the file WordCountjava Your code should g

Write your code in the file WordCount.java. Your code should go into a method with the following signature. You may write your own main method to test your code. The graders will ignore your main method: public static int countWords(String original, int minLength){} Your method should count the number of words in the sentence that meet or exceed minLength (in letters). For example, if the minimum length given is 4, your program should only count words that are at least 4 letters long. Words will be separated by one or more spaces. Non-letter characters (spaces, punctuation, digits, etc.) may be present, but should not count towards the length of words. Hint: write a method that counts the number of letters (and ignores punctuation) in a string that holds a single word without spaces. In your countWords method, break the input string up into words and send each one to your method.

Solution

WordCount.java


import java.util.Scanner;

public class WordCount {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the string: \");
       String s = scan.nextLine();
       System.out.println(\"Enter the min length: \");
       int n = scan.nextInt();
       int count = countWords(s,n);
       System.out.println(\"Number of words is \"+count);
      
   }
   public static int countWords(String original, int minLength){
       String words[] = original.split(\"\\\\s+\");
       int wordCount = 0;
       for(int i=0; i<words.length; i++){
           int letterCount = 0;
           for(int j=0; j<words[i].length(); j++){
               char ch = words[i].charAt(j);
               if((ch>=\'a\'&&ch<=\'z\') || (ch>=\'A\' && ch<=\'Z\')){
                   letterCount++;
               }
           }
           if(letterCount >= minLength){
               wordCount++;
           }
       }
       return wordCount;
   }
}

Output:

Enter the string:
Hi, Good morning. How is the day. Great day ahead. keep going.
Enter the min length:
4
Number of words is 6

Write your code in the file WordCount.java. Your code should go into a method with the following signature. You may write your own main method to test your code

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site