CSE143 Sample Program handout #15 Program WordCount.java ---------------------- // Stuart Reges // 10/29/05 // // This program prompts the user for the name of a file and then counts the // occurrences of words in the file (ignoring case). It then reports the // frequencies using a cutoff supplied by the user that limits the output // to just those words with a certain minimum frequency. import java.util.*; import java.io.*; public class WordCount { public static void main(String[] args) throws FileNotFoundException { // open the file Scanner console = new Scanner(System.in); System.out.print("What is the name of the text file? "); String fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName)); // count occurrences SortedMap<String, Integer> wordCounts = new TreeMap<String, Integer>(); while (input.hasNext()) { String next = input.next().toLowerCase(); if (!wordCounts.containsKey(next)) { wordCounts.put(next, 1); } else { wordCounts.put(next, wordCounts.get(next) + 1); } } // get cutoff and report frequencies System.out.print("Minimum number of occurrences for printing? "); int min = console.nextInt(); for (String word : wordCounts.keySet()) { int count = wordCounts.get(word); if (count >= min) System.out.println(count + "\t" + word); } } } Sample Log of Execution ----------------------- What is the name of the text file? WordCount.java Minimum number of occurrences for printing? 2 2 "); 3 + 10 // 2 1); 7 = 3 a 2 and 2 cutoff 3 file 3 for 2 frequencies 2 if 2 import 2 int 2 name 3 new 3 occurrences 4 of 2 public 2 scanner 2 string 10 the 2 then 2 user 2 wordcounts 2 wordcounts.put(next, 2 words 6 { 6 } Another Sample Log of Execution ------------------------------- What is the name of the text file? moby.txt Minimum number of occurrences for printing? 500 4571 a 1354 all 587 an 6182 and 563 are 1701 as 1289 at 973 be 1691 but 1133 by 1522 for 1067 from 754 had 741 have 1686 he 552 him 2459 his 1746 i 3992 in 512 into 1555 is 1754 it 562 like 578 my 1073 not 506 now 6408 of 933 on 775 one 675 or 882 so 599 some 2729 that 14092 the 602 their 506 there 627 they 1239 this 4448 to 551 upon 1567 was 644 were 500 whale 552 when 547 which 1672 with 774 you
Stuart Reges
Last modified: Fri Feb 8 15:32:35 PST 2008