我正在尝试计算与特定 url 地址相关联的单词的数量,并按照它们出现的数量顺序打印这些单词。现在它读入文件并计算单词,但它会逐行打印出来,而不是计算整个段落。任何帮助它阅读整个内容并打印出整个段落中的单词以及它们的出现都会很棒。
public static void main(String[] args)
{
try {
URL url = new URL("http://webpagehere.txt");
BufferedReader reader = new BufferedReader
(new InputStreamReader(url.openStream()));
String line;
int i=0;
while ((line = reader.readLine()) != null) {
i++;
System.out.println("Line " + i + "\t" + line);
// Create a TreeMap to hold words as key and count as value
Map<String, Integer> map = new TreeMap<String, Integer>();
String[] words = line.split("[ \n\t\r.,;:!?(){}]");
for (int j = 0; j < words.length; j++) {
String key = words[j].toLowerCase();
if (key.length() > 0) {
if (!map.containsKey(key)) {
map.put(key, 1);
}
else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
}
// Get all entries into a set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
// Get key and value from each entry
for (Map.Entry<String, Integer> entry: entrySet)
System.out.println(entry.getKey() + "\t" + entry.getValue());
}
reader.close();
} catch (UnknownHostException e) {
JOptionPane.showMessageDialog(null, "Unknown host. Abort.");
} catch (NoRouteToHostException e) {
JOptionPane.showMessageDialog(null,
"Cannot reach remote host. Abort.");
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}