1
Student Name: __________________
Class and Section __________________
Total Points (15 pts) __________________
Due: Wednesday, Sep 12, 2012 before the class
Project: Counting the Occurrences of Words
CSCI 2410 Data Structures and Algorithms
Armstrong Atlantic State University
Problem Description:
Rewrite Listing 21.9 to read the text from a Web page. The URL for the Web
page is passed as a command-line argument. Words are delimited by whitespace,
punctuation marks (,;.:?), quotation marks (’”), and parentheses. Count words in
Please see page 551 for how to read data from a URL.
Analysis:
(Describe the problem including input and output in your own words.)
2
Design:
(Describe the major steps for solving the problem. How do you use recursion to solve this
problem.)
Testing: (Describe how you test this program)
What to submit?
3
1. Compile and Submit to LiveLab (you must submit the program regardless whether it
complete or incomplete, correct or incorrect)
2. Print this copy.
3. Fill in self-evaluation:
1. Can your program display words only? _______________
2. Can your program display words in increasing order? _______________
3. Can your program count words correctly? _______________
4. Can your program pass argument from the command-line? _______________
Solution:
import java.util.*;
import java.io.*;
// Create a tree map to hold words as key and count as value
TreeMap<String, Integer> treeMap = new TreeMap<>();
try {
java.net.URL url = new java.net.URL(args[0]);
Scanner input = new Scanner(url.openStream());
while (input.hasNext()) {
String line = input.nextLine();
String[] words = line.split(“[ @!~{}\\[\\]$#^&*\n\t\r.,;?’\”)(]”);
4
else {
treeMap.put(key, 1);
}
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
// Get an entry set for the tree map
Set<Map.Entry<String, Integer>> entrySet = treeMap.entrySet();