Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how can fixe thes code [i} ? private static double averageStudentWordLength(String[] sentence){ double sumOfLengths = 0; for(int i = 0;i < sentence.length;i++);{ sumOfLengths = sumOfLengths

how can fixe thes code [i} ?

private static double averageStudentWordLength(String[] sentence){ double sumOfLengths = 0; for(int i = 0;i < sentence.length;i++);{ sumOfLengths = sumOfLengths + sentence[i].length(); }

package c17; import java.text.DecimalFormat; import java.util.Scanner; /**This program reads sentences from user and print average length**/ public class AverageStudentWordLengthMain {

public static void main(String[] args) { //Get user input using scanner Scanner sc = new Scanner(System.in); System.out.print("Enter number of students in the class:"); int noOfStudents = sc.nextInt(); sc.nextLine(); //Declare name array String[] names= new String[noOfStudents]; //Declare student array String[][] sentences = new String[noOfStudents][1]; StringBuilder sentence = new StringBuilder(); //Iterate for noOfStudents for(int i = 0; i < noOfStudents; i++){ System.out.print("Enter student " + (i + 1) + "name and his sentence(end with STOP):"); String line = sc.nextLine(); String[] lineArr = line.split(" "); int length = lineArr.length; String studName = lineArr[0]; //Skip last word for(int j = 1;j < length;j++){ if(lineArr[j].equalsIgnoreCase("STOP")) continue; sentence.append(lineArr[j] +" "); } //Assign sentence and name to array sentences[i][0] = sentence.toString(); names[i] = studName; sentence = new StringBuilder(); } //Call to get average arrays double avg[] = averageAllStudentWordLength(sentences); //Call to get maxLength double maxLength = maxaverageWordLength(avg); System.out.println(""); DecimalFormat df = new DecimalFormat(".00"); String studentWithMaxLength = null; System.out.println(">>>>>>>>>>>>>>>>>>|nThe class has " + noOfStudents + " students and here are their sentences:"); for(int i = 0;i < sentences.length;i++){ System.out.println("Student " + (i + 1) + " name: " + names[i] + ",his average word length: " + df.format(avg[i]) + ", and his sentence is:" + sentences[i][0]); //Get student name for maxLength if(avg[i] == maxLength) studentWithMaxLength = names[i]; } System.out.println("student " + studentWithMaxLength + " got the maximum average word length: " + df.format(maxLength)); sc.close(); } //Returns average of given array private static double averageStudentWordLength(String[] sentence){ double sumOfLengths = 0; for(int i = 0;i < sentence.length;i++);{ sumOfLengths = sumOfLengths + sentence[i].length(); } return sumOfLengths / sentence.length; } //Returns average double array private static double[] averageAllStudentWordLength(String[][] allStudentsSentence) { double [] avgLength=new double[allStudentsSentence.length]; for(int i = 0;i < allStudentsSentence.length;i++){ String[] sentenceArr=allStudentsSentence[i][0].split(" "); double avg = averageStudentWordLength(sentenceArr); avgLength[i] = avg; } return avgLength; } //Returns max length given array private static double maxaverageWordLength(double[] averageWordLengths){ double max = 0; for(int i=0;i < averageWordLengths.length;i++){ if(averageWordLengths[i] > max) max = averageWordLengths[i]; } return max;

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

8. How would you explain your decisions to the city council?

Answered: 1 week ago