-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise12_19.java
45 lines (40 loc) · 1.59 KB
/
Exercise12_19.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ch_12;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 12.19 (Count words) Write a program that counts the number of words in President
* Abraham Lincoln’s Gettysburg address from http://cs.armstrong.edu/liang/data/
* Lincoln.txt. (Substituted http://se.cs.depaul.edu/Java/Chapter04/Lincoln.txt)
*/
public class Exercise12_19 {
public static void main(String[] args) {
try {
URL url = new URL("http://se.cs.depaul.edu/Java/Chapter04/Lincoln.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("Number of words in the Gettysburg Address is " + countWords(bufferedReader));
} catch (MalformedURLException malformedURLException) {
malformedURLException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
static int countWords(BufferedReader br) throws IOException {
int wordCount = 0;
String line;
while ((line = br.readLine()) != null) {
String[] words = line.split("\\s");
if (words.length == 0) continue;
for (String word : words) {
word = word.trim();
if (word.length() > 0 && !word.equals(" ")) {
wordCount++;
// System.out.println("Word: " + word + " Count = " + wordCount);
}
}
}
return wordCount;
}
}