iam編寫此代碼來查找跨文件的前20個常用詞我已閱讀 但在第51行和第20行得到錯誤作爲空點異常 ,我無法找到它 我需要什麼變化 使 基於命令行參數的iam讀取文件 也提供了一些幫助來添加條件 先謝謝您。java編碼查找跨文件20經常使用的詞
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;
}
}
}
}
}
我只花10分鐘試圖重新格式化你的代碼...所以我不會幫你。請指出20和51行是什麼,因爲這裏的行號不一樣。並複製/粘貼異常。 – lpratlong
你介意告訴我們你的計劃的行號是什麼? 20和51! –