Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2 *******You may NOT use any modules or classes from outside java.lang (

ID: 3759535 • Letter: P

Question

Problem 2 *******You may NOT use any modules or classes from outside java.lang (do not add any "import" statements to your programs). ****** I HAVE TO USE JAVA IO 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

Explanation / Answer

Answer :

import java.util.*;
public class StringProgram
{
static int i,c=0,res;
static int wordcount(String s,int minlength)
{

   String[] words = s.split(" ");
  
System.out.println("The number of words is:"+words.length);
  
for(int i=0; i<words.length; i++){
   if(words[i].length()==4)
//System.out.println(words[i]+" is "+words[i].length()+" letters long.");
   c++;
   }

return c;
}
  
public static void main (String args[])
{
   int minlen=4;
   Scanner in = new Scanner(System.in);
System.out.println("Please type a phrase.");
String phrase= in.nextLine();
res=StringDemo.wordcount(phrase,minlen);
//string is always passed in double quotes
  
System.out.println("The number of words in the String with minimum length 4 are :

"+res);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote