【问题标题】:WordCount with treemap带树状图的字数
【发布时间】:2014-07-03 04:57:21
【问题描述】:

我正在编写一个程序来读取文本文件,存储在 treeMap 中,然后将单词 frequences(wordcount) 打印到控制台。我不断收到 FileNotFoundException "I THINK" 我几乎完成了,剩下的代码。任何帮助、指示、建议和提示将不胜感激。谢谢。代码如下

导入 java.util.*;

/** * * @作者 * */

公共类字数{

public static void main(String[] args) {
    // TODO Auto-generated method stub
    TextFileInput take = new TextFileInput("noteFile.txt");

    String m = take.readLine();
    String [] input = m.split("[ \n\t\r,.;:!?(){}}]");

TreeMap <String, Integer> myMap  = new TreeMap <String, Integer> ();

    /**Set set = myMap.entrySet(); 
    Iterator i = set.iterator(); 
    Map.Entry <String, Integer> me; **/

    for(int f = 0; f < input.length; f++) {         
        String key = input[f].toUpperCase();
        if(input[f].length() > 1) {
            if(myMap.get(key) == null) {
                myMap.put(key, 1);
                }   
            else {
                    int value = myMap.get(key).intValue();
                    value++;
                    myMap.put(key, value);
            }
        }       
    }       
    /**while(i.hasNext()) { 
           me = (Map.Entry)i.next(); 
           System.out.print(me.getKey() + ": "); 
           System.out.println(me.getValue()); **/


    for(Map.Entry<String, Integer> entry : myMap.entrySet()) {
        System.out.println(entry.getKey() + " : "+ entry.getValue());
    }

}
}

}

【问题讨论】:

  • 我不确定您的问题或实际代码中是否有错误,但最后您有一些额外的}。还有“我不断收到FileNotFoundException”你是如何运行你的代码的?您的控制台在哪个位置?您要读取的文件位于何处? "...and some others" 还有哪些错误?
  • 找到的文件在note文件通常的位置。它在 JRE 系统的正下方,但实际上不在其中。我不知道这个地方的名字是什么(对不起)。我将我的代码作为 Java 文件运行。我使用 TextFileInput 读取代码。

标签: java regex text-files treemap


【解决方案1】:

TextFileInput - 我不确定。您可以使用文件和扫描仪从文件中读取。 给出文件的绝对路径。例如。 C://notepad.txt(适用于 windows)

此外,您正在从文件中读取一行。您可以将其添加到 while 循环中。要打印 TreeMap,您可以执行以下操作,

 for(String entry : myMap.keySet()) {
    System.out.println(entry + " : "+ myMap.get(entry));
}

完整的代码如下,

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class WordCount {

public static void main(String[] args) throws FileNotFoundException {

    File file = new File("C://notepad.txt");
    Scanner scanner=new Scanner(file);

    TreeMap <String, Integer> myMap  = new TreeMap <String, Integer> ();

    while(scanner.hasNext())
    {
    String m = scanner.nextLine();
    String [] input = m.split("[ \n\t\r,.;:!?(){}}]");

    for(int f = 0; f < input.length; f++) {         
        String key = input[f].toUpperCase();
        if(input[f].length() > 1) {
            if(myMap.get(key) == null) {
                myMap.put(key, 1);
                }   
            else {

                    myMap.put(key, (myMap.get(key))+1);
            }
        }       
    }       
    }

    for(String entry : myMap.keySet()) {
        System.out.println(entry + " : "+ myMap.get(entry));
    }

}

}

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2017-03-06
    • 2016-10-26
    • 2021-10-04
    • 2013-08-04
    • 2012-05-02
    • 2021-06-07
    • 2012-12-16
    相关资源
    最近更新 更多