0

我正在尝试计算与特定 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();
    }

}
}
4

2 回答 2

1

您只需将初始化移动到Mapwhile 循环外部的打印。

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();
        }

    }
于 2013-03-25T17:26:19.837 回答
0

map要存储的单词及其出现在 while 循环中声明。只需将声明移出 while 循环即可使其工作。

        int i = 0;
        Map<String, Integer> map = new TreeMap<String, java.lang.Integer>();
        while ((line = reader.readLine()) != null) {
于 2013-03-25T17:27:40.773 回答