i'm kind of stuck on this issue. I'm trying to store 1 or more words in a single string, find the length of all the words combined then divide it by the number of words to find the average. I'm required to do this in a while loop (that's the objective of the homework)
When I enter "Hello my name is" it returns the length as
Result:
5
2
4
2
But what I want is to add those results then divide it by the amount of words
5+2+4+2 = 13
13/4 = 3.25
This is what I have so far:
Scanner in = new Scanner(System.in);
int counter = 0;
double sum = 0;
while (in.hasNext()) {
String word = in.next();
double totalchar = word.length();
sum = totalchar + sum;
counter++;
double average = 0;
if (counter > 0) {
average = sum / counter;
}
System.out.println(average);
}