java编码在文件中查找20个频繁使用的单词

java编码在文件中查找20个频繁使用的单词,java,Java,我写这段代码是为了找到我读过的文件中最常用的20个单词 但是我在第51行和第20行中得到了一个错误作为空点异常 我找不到它 我需要什么改变 使 iam基于命令行参数读取文件 还为我提供了一些帮助来添加条件 先谢谢你 import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.ArrayList; import java.util.Collections; i

我写这段代码是为了找到我读过的文件中最常用的20个单词 但是我在第51行和第20行中得到了一个错误作为空点异常 我找不到它 我需要什么改变 使 iam基于命令行参数读取文件 还为我提供了一些帮助来添加条件 先谢谢你

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.StringTokenizer;


public class WordFrequency {
    private static final Hashtable<String, Integer> Null = null;    

    public static void main(String[] args) throws IOException {
        System.out.println("in to the main");
        WordFrequency test = new WordFrequency();
        test.countWordFre(args); // for accessing the countWordFre method
    }

    // countWordFre method      
    public void countWordFre(String[] fileNames) throws IOException {

        int i;

        Scanner fileReader = null;

        Hashtable<String, Integer> map = new Hashtable<String, Integer>();

        System.out.println("reading files");

        System.out.println("enter how many number of files to read");
        System.out.println(" enter the filename");

        // as command line arguments

        for ( i = 0 ; i < fileNames.length ; i++ ) {

            fileReader = new Scanner(new FileInputStream(fileNames [i]));
        }


        while (fileReader.hasNextLine()) {

            String line = fileReader.nextLine();

            String word;
            // to read each tokens of a line

            StringTokenizer st = new StringTokenizer(line);


            while (st.hasMoreTokens()) {

                word = st.nextToken().toLowerCase();

                System.out.println("converting the words to lower case");   


                if (map.containsKey(word)) {

                    int count = (Integer) map.get(word);

                    map.put(word, count + 1);

                } else {

                    map.put(word, 1);

                }

            }

        }

        Enumeration<String> e = map.keys();

        while (e.hasMoreElements()) {

            String word = e.nextElement();

            System.out.println(word + " " + map.get(word));
        } 
    }
   // System.out.println(map.size());




    public void List() throws IOException

    {

        System.out.println("to find 20 frq word");


        Hashtable<String, Integer> map = Null;



        List<String> mapKeys = new ArrayList<String>();

        List<Integer> mapValues = new ArrayList<Integer>(map.values());  


        Collections.sort(mapValues);
        Collections.reverse(mapValues); //descending order sort
        Collections.sort(mapKeys);
        Collections.reverse(mapValues);

        LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); 

        Iterator<Integer> valueIt = mapValues.iterator();

        while (valueIt.hasNext()) {

            Object val = valueIt.next();


            Iterator<String> keyIt = mapKeys.iterator();

            while (keyIt.hasNext()) {

               Object key = keyIt.next();

               String valueFromOriginalMap = map.get(key).toString();

               String valueFromCurrentIteration = val.toString();

               if (valueFromOriginalMap .equals(valueFromCurrentIteration )) {                                                 

                    map.remove(key);                                                    
                    mapKeys.remove(key);                                             
                    sortedMap.put((String)key , (Integer)val);

                    break;
               }       


           }

     }

  }

}

这可能是因为您没有正确地将参数传递给程序,因此我在没有参数的情况下也会遇到相同的错误。使用正确的args,它运行良好

确保在cmd中正确传递参数

java WordFrequency "Hello.txt"

从编写一个更小更简单的程序开始,从控制台读入一个文件名。2以文本形式打开该文件。3在屏幕上显示文件的前20个字

彻底测试那个较小的程序,确保它工作正常。例如,如果键入文件名时出错,它会做什么?比如说,myfile.ttx而不是myfile.txt

当较小的程序运行时,将其展开以计算单个文件中最常见的20个字。再次测试并修复任何问题。然后才尝试编写最后一个程序来计算多个文件中的字数

让第一个小程序开始工作应该会告诉您当前程序失败的地方,但因为程序较小,所以更容易看到。

您的代码

    for ( i = 0 ; i < fileNames.length ; i++ ) {
        fileReader = new Scanner(new FileInputStream(fileNames [i]));
    }
    while (fileReader.hasNextLine()) {

我只花了10分钟重新格式化你的代码。。。所以我不会帮你。请指出第20行和第51行是什么,因为这里的行号不同。和复制/粘贴例外。您能告诉我们您的程序的第20行和第51行吗!
            Integer freq = map.get(word);
            int count = freq == null ? 0 : freq;
            map.put(word, count + 1);