Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.duke.*;
- public class WordLengths {
- public void countWordLengths(FileResource fr, int[] counts) {
- for (String s : fr.words()) {
- int wadu = s.length();
- for (int k = 0; k < wadu; k++) {
- if (Character.isLetter(s.charAt(k)) == false) {
- wadu--;
- }
- }
- if (wadu >= 30) {
- counts[30]++;
- } else {
- counts[wadu]++;
- }
- }
- for (int i = 0; i < 31; i++) {
- if (counts[i] != 0) {
- System.out.println("Words with length " + i + " : " + counts[i]);
- }
- }
- int max[] = counts;
- System.out.println("Most number of words for one length: " + indexOfMax(max));
- }
- public void testCountWordLengths() {
- FileResource fr = new FileResource("smallHamlet.txt");
- int[] counts = new int[31];
- countWordLengths(fr, counts);
- }
- public int indexOfMax(int[] values) {
- int max = 0;
- for (int i = 0; i < 31; i++) {
- if (values[i] >= max) {
- values[i] = max;
- }
- }
- return max;
- }
- public static void main(String[] args) {
- WordLengths run = new WordLengths();
- run.testCountWordLengths();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement