import?java.io.*;?//在測試時,這里有錯誤:無法解析java.util.Map$Entry.從必需的.class文件間接引用了它,怎么修改呢?
import?java.util.ArrayList;
import?java.util.Collections;
import?java.util.HashMap;
import?java.util.Iterator;
import?java.util.List;
import?java.util.Map;
??
import?javax.swing.JFrame;
import?javax.swing.JTextArea;
??
public?class?T4?{
??
????@SuppressWarnings("unchecked")
????public?static?void?main(String[]?args)?throws?IOException?{
??
????????String?fileName?=?"Hello.txt";
????????RandomAccessFile?file?=?new?RandomAccessFile(new?File(fileName),?"r");
??????????
????????String?content?=?null;
??????????
????????Map<String,?Integer>?map?=?new?HashMap<String,?Integer>();
??????????
????????while((content?=?file.readLine())?!=?null){
??????????????
????????????String[]?ary?=?content.replaceAll("'|\\?|,|\\.|",?"").split("\\s+");
??????????????
????????????for(String?str?:?ary){
????????????????if(map.containsKey(str.trim())){
????????????????????map.put(str,?new?Integer(map.get(str).intValue()?+?1));
????????????????}else{
????????????????????map.put(str.trim(),?new?Integer(1));
????????????????}
????????????}
????????}
??????????
????????List?list?=?new?ArrayList();
?????????/*下面for循環(huán)顯示錯誤:此行的多個標記-?Iterator?是原始類型。應(yīng)該將對通用類型?Iterator<E>?的引用參數(shù)化
????????????????????????????????????????????????-類型?Map<String,Integer>?中的方法?entrySet()引用缺少的類型?Map$Entry*/?
????????for?(Iterator?iter?=?map.entrySet().iterator();?iter.hasNext();)?{??????????????????????????????????????????????????????????????????
??????????Map.Entry<String,?Integer>?element?=?(Map.Entry<String,?Integer>)?iter.next();
??????????list.add(new?WordFreq(element.getKey(),?element.getValue()));
????????}
????????/*下面一行顯示的錯誤是:無法解析類型?java.util.Comparator。從必需的?.class?文件間接引用了它
????????????????????????????????-?Comparator?無法解析為類型
????????????????????????????????-?類型?Collections?中的方法?sort(List<T>,?Comparator<??super?T>)引用缺少的類型?
?Comparator?*/
????????Collections.sort(list,?new?Comparator<WordFreq>(){
????????public?int?compare(WordFreq?o1,?WordFreq?o2)?{
???????????return?o1.getCount()?<?o2.getCount()??1:?-1;
????????}
????????});
??????????
????????int?uniqueCount?=?0;
????????final?StringBuilder?sb?=?new?StringBuilder();
????????final?String?NEW_LINE?=?"\r\n";
??????????
????????for(Object?obj:?list){
????????????WordFreq?item?=?(WordFreq)?obj;
????????????uniqueCount?+=?(item.getCount()?==?1??1:?0);
????????????sb.append(item.toString());????????//這里顯示的錯誤是:無法解析類型?java.lang.CharSequence?從必需的?.class?文件間接引用了它
????????????sb.append(NEW_LINE);
????????}
??????????
????????JFrame?f?=?new?JFrame();
????????JTextArea?result?=?new?JTextArea();
??????????
????????result.append("Total?"?+?list.size()?+?"?words?found?in?"?+?fileName);
????????result.append(NEW_LINE);
????????result.append("Total?"?+?uniqueCount?+?"?words?appear?only?once");
????????result.append(NEW_LINE);
????????result.append(NEW_LINE);
????????result.append(sb.toString());
??????????
????????f.add(result);
????????f.setVisible(true);
????????f.pack();
????????f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
??????????
????}
??
}
??
class?WordFreq{
????private?String?word;
????private?int?count;
??????
????public?WordFreq(String?word,?int?count){
????????this.word?=?word;
????????this.count?=?count;
????}
??
????public?int?getCount()?{
????????return?count;
????}
??????
????public?String?toString(){
????????return?word?+?"\t"?+?count;
????}
??????
}
添加回答
舉報
0/150
提交
取消