import java.util.Scanner; /* * Write a program to find the average number of words in an array of sentence strings */ pu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

import java.util.Scanner; /* * Write a program to find the average number of words in an array of sentence strings */ pu

Post by answerhappygod »

Import Java Util Scanner Write A Program To Find The Average Number Of Words In An Array Of Sentence Strings Pu 1
Import Java Util Scanner Write A Program To Find The Average Number Of Words In An Array Of Sentence Strings Pu 1 (186.79 KiB) Viewed 56 times
Pls, help me solve this. Tks
import java.util.Scanner; /* * Write a program to find the average number of words in an array of sentence strings */ public class AverageWord Count { public static void main(String[] args) { // array of sentences String sentences [] = {"Yeah, I'm gonna take my horse", "To the old town road", "I'm gonna ride til I can't no more", "I'm gonna take my horse to the old town road", "I'm gonna ride til I can't no more"}; int totalWords = 0; double averageWords = 0.0; for (int i = 0; i < sentences. length; i++) { totalWords += wordCount(sentences); // ----Start below here. To do: approximate lines of code = 2 // compute the average number of words in a sentence and store in the variable averageWords // Hint: test make sure you do not divide by 0. You will need to cast averageWords = totalWords; //--- -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. System.out.println("Average number of words per sentence: " + averageWords); System.out.println("Expected:\nAverage number of words per sentence: 7.4"); } // Calculate the number of words in the input String sentence. Return the number of words. static int wordCount(String, sentence) { // -Start below here. To do: approximate lines of code = 7 // There are several ways to solve this. One basic way is to use a Scanner, then in a while loop read the words one by // one and increase the word count. Another way is to use the StringTokenizer class. // A third way is to use the split() method in class String -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } }
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply