I want to count only words that have 3 letters in them. For example, if the user
ID: 3631226 • Letter: I
Question
I want to count only words that have 3 letters in them. For example, if the user inputs: "My name is Bob Pickens." Therefore, the output would be: 3I've created a way to count each word and letters in a string, but I don't know how to make it only count words with only 3 letters. Where do I go from this?
public class Word {
//count letters
public void count(String words){
if (words ==null)
return;
int counter=0;
for (int i=0;i<words.length(); i++){
if (Character.isLetter(words.charAt(i)))
counter++;
}
}
public static void main(String[] args){
//count words
System.out.println("Please enter anything");
String string= userInput.nextLine()
//character limit
int count= 3
int a= sentence.length();
int words=1;
for (int i= 0; i< a; i++)
{if(sentence.charAt(i)== ' ')
words += 1;
else
;
}
System.out.println( words +"words ");
}
}
Explanation / Answer
/*logic used to count three letter words is if a space is encountered then chack if number of letters in hte word prior to space is 3. if yes increase count else leave. but in both the cases make let_counter=0 which is letter counter in a word.} public class WordCounter{ public void count(String words){ if (words ==null) return; int counter=0; int let_counter; for (int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.