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

In Java: Write a program (method main) using an array to solve the following pro

ID: 3779223 • Letter: I

Question

In Java:

Write a program (method main) using an array to solve the following problem:

• Prompt the user to enter a String of data • Using the charAt(index) method in the String class to access each character in the string individually, produce a count of the number of word lengths • Count only letters (regardless of upper or lower case) and treat spaces, numbers and punctuation as word separators. • Display the number of words and lengths for all non-zero lengths, and the total number of words

Example 1: (green is user input)
Enter the phrase: Pneumonoultramicroscopicsilicovolcanoconiosis is the longest word in the dictionary

2 2 letter words

2 3 letter words

1 4 letter words

1 7 letter words

1 10 letter words

1 45 letter words

8 total words

Example 2:
Enter the phrase: one,two,three,four,five,six,seven,eight,nine,ten,1234567890!@#$%^&*()_+

4 3 letter words

3 4 letter words

3 5 letter words

10 total words

Example 3:
Enter the phrase : The    Quick    Brown Fox     Jumps Over The Lazy Dog

4 3 letter words

2 4 letter words

3 5 letter words

9 total words

Explanation / Answer

Hey, the program for it is as follows:

import java.util.Scanner;

public class WordCounter {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       System.out.println("Enter the phrase");
       Scanner scanner = new Scanner(System.in);
       System.out.flush();
       int arr[]=new int[200];
       String phrase= scanner.nextLine();
       int count=0;
       for(int i=0;i<phrase.length();i++){
           if(Character.isAlphabetic(phrase.charAt(i))){
               count++;
               if(i==(phrase.length()-1)){
                   arr[count]++;
               }
           }
           else
           {
               arr[count]++;
               count=0;
           }
       }
       int total=0;
       for(int j=1;j<200;j++){
           if(arr[j]!=0){
               System.out.println(""+arr[j] + " " + j + " letter words") ;
               total+=arr[j];
           }
       }
       System.out.println(total + " total words");
      
      
      
   }

}

Sample run:

Enter the phrase
The Quick Brown Fox Jumps Over The Lazy Dog
4 3 letter words
2 4 letter words
3 5 letter words
9 total words

Enter the phrase
Pneumonoultramicroscopicsilicovolcanoconiosis is the longest word in the dictionary
2 2 letter words
2 3 letter words
1 4 letter words
1 7 letter words
1 10 letter words
1 45 letter words
8 total words

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