我需要从一个特定的url地址打印出每个单词在文章中使用的次数

  • 本文关键字:单词 文章 打印 url 一个 地址 java
  • 更新时间 :
  • 英文 :


我正试图计算与特定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("[ ntr.,;:!?(){}]");
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();
    }
}
}

您只需要将Map的初始化和打印移出while循环。

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("[ ntr.,;:!?(){}]");
                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();
        }
    }

您的map用于存储单词,其出现情况在while循环中声明。只需将声明移出while循环即可使其工作。

        int i = 0;
        Map<String, Integer> map = new TreeMap<String, java.lang.Integer>();
        while ((line = reader.readLine()) != null) {

最新更新