Question: In Java. Write a method that returns the average length of all words in the string str. Use the code below to help with this

In Java. Write a method that returns the average length of all words in the string str. Use the code below to help with this method

In Java. Write a method that returns the average length of all

class StringWords {

public static void main(String[] args) {

String str = "Mary had a little lamp";

int c = countWords(str);

System.out.println("Words : " + c);

}

private static int countWords(String aStr) {

String temp[] = aStr.split(" ");

int c = 0;

for (String s : temp) {

if (s.length() > 0)

c++;

}

return c;

}

}

class StringWords { Run Debug public static void main(String[] args) { String str = "Mary had a little lamp"; int c = countWords (str); System.out.println("Words : " + c); private static int countWords (String astr) { String temp[] = aStr.split(" "); int c = 0; for (Strings : temp) { if (s.length() > 0) C++; return c

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!