Question: Whates wrong with this code and please point out what i did wrong? package c17; import java.text.DecimalFormat; import java.util.Scanner; /**This program reads sentences from user

Whates wrong with this code and please point out what i did wrong?

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 { 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 { 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 { 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 { 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 { 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 { if(averageWordLengths[i]>max) max=averageWordLengths[i]; } return max;

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!