我正在尝试计算与特定网址相关联的字词数,并按照它们出现的出现次数按顺序打印字词。现在它读取文件并计算单词,但是它逐行打印出来,而不是整个段落的计数。任何帮助,让它阅读整个事情并打印出来的话 在它们出现的整个段落中都会很棒。
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();
}
}
}
答案 0 :(得分:1)
您只需要在while循环之外的Map
打印初始化。
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;
Map<String, Integer> map = new TreeMap<String, Integer>();
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
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);
}
}
}
}
reader.close();
// 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());
} catch (UnknownHostException e) {
System.out.println("Unknown host. Abort.");
} catch (NoRouteToHostException e) {
System.out.println("Cannot reach remote host. Abort.");
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
您的map
存储单词及其出现在while循环中声明。只需将声明移出while循环即可使其正常工作。
int i = 0;
Map<String, Integer> map = new TreeMap<String, java.lang.Integer>();
while ((line = reader.readLine()) != null) {