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

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
Get step-by-step solutions from verified subject matter experts
