Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- Determines the average number of characters for words in a string.
- Input: a string of one or more lines containing words
- Output: the average number of characters per word in the string
- to two decimal places
- */
- public class WordAvg
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- int numOfWords = 0;
- int avgLength = 0;
- while (in.hasNext())
- {
- String word = in.next();
- avgLength += word.length();
- numOfWords++;
- }
- // more work here
- System.out.printf("%.2f\n", (float)avgLength / (float)numOfWords);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement