编写一个Java应用程序,使用RandomAccessFile流统计Hello.txt中的单词,要求如下:
(1)计算全文中共出现了多少个单词(重复的单词只计算一次);(2)统计出有多少个单词只出现了一次;(3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示...
(1)计算全文中共出现了多少个单词(重复的单词只计算一次);
(2)统计出有多少个单词只出现了一次;
(3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。
大侠们,帮帮小弟!!! 展开
(2)统计出有多少个单词只出现了一次;
(3)统计并显示出每个单词出现的频率,并将这些单词按出现频率高低顺序显示在一个TextArea中。
大侠们,帮帮小弟!!! 展开
4个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class test
{
public static void main(String[] args) throws Exception
{
StringBuffer sb = new StringBuffer();
RandomAccessFile raf = new RandomAccessFile(new File("D:\\workspace\\test\\src\\test.java"),"r");
String str = null;
while((str = raf.readLine()) != null)
{
sb.append(str);
}
raf.close();
String content = sb.toString();
String[] wordArray = content.split("[^a-zA-Z]");
Map<String,Integer> map = new HashMap<String,Integer>();
for(String word:wordArray)
{
if(!word.equals(""))
{
if(map.containsKey(word))
{
map.put(word, Integer.parseInt(map.get(word).toString())+1);
}
else
{
map.put(word, 1);
}
}
}
System.out.println("单词数量:"+map.size());
Set set = map.entrySet();
List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>();
for(Iterator i = set.iterator();i.hasNext();)
{
Map.Entry<String,Integer> entry = (Map.Entry<String,Integer>)i.next();
if(entry.getValue().toString().equals("1"))
{
System.out.println(entry.getKey()+":出现一次");
}
list.add(entry);
}
//排序
Collections.sort(list,new WordComparator());
for(Map.Entry<String,Integer> entry:list)
{
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
class WordComparator implements Comparator
{
public int compare(Object arg0, Object arg1)
{
Map.Entry<String,Integer> map1 = (Map.Entry<String,Integer>)arg0;
Map.Entry<String,Integer> map2 = (Map.Entry<String,Integer>)arg1;
return Integer.parseInt(map2.getValue().toString()) - Integer.parseInt(map1.getValue().toString());
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class test
{
public static void main(String[] args) throws Exception
{
StringBuffer sb = new StringBuffer();
RandomAccessFile raf = new RandomAccessFile(new File("D:\\workspace\\test\\src\\test.java"),"r");
String str = null;
while((str = raf.readLine()) != null)
{
sb.append(str);
}
raf.close();
String content = sb.toString();
String[] wordArray = content.split("[^a-zA-Z]");
Map<String,Integer> map = new HashMap<String,Integer>();
for(String word:wordArray)
{
if(!word.equals(""))
{
if(map.containsKey(word))
{
map.put(word, Integer.parseInt(map.get(word).toString())+1);
}
else
{
map.put(word, 1);
}
}
}
System.out.println("单词数量:"+map.size());
Set set = map.entrySet();
List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>();
for(Iterator i = set.iterator();i.hasNext();)
{
Map.Entry<String,Integer> entry = (Map.Entry<String,Integer>)i.next();
if(entry.getValue().toString().equals("1"))
{
System.out.println(entry.getKey()+":出现一次");
}
list.add(entry);
}
//排序
Collections.sort(list,new WordComparator());
for(Map.Entry<String,Integer> entry:list)
{
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
class WordComparator implements Comparator
{
public int compare(Object arg0, Object arg1)
{
Map.Entry<String,Integer> map1 = (Map.Entry<String,Integer>)arg0;
Map.Entry<String,Integer> map2 = (Map.Entry<String,Integer>)arg1;
return Integer.parseInt(map2.getValue().toString()) - Integer.parseInt(map1.getValue().toString());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JDK 1.5
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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 Du2 {
@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 (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()));
}
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());
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;
}
}
---------testing, content in text area:
Total 5 words found in Hello.txt
Total 4 words appear only once
you 2
fine 1
How 1
are 1
thank 1
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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 Du2 {
@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 (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()));
}
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());
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;
}
}
---------testing, content in text area:
Total 5 words found in Hello.txt
Total 4 words appear only once
you 2
fine 1
How 1
are 1
thank 1
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个题不难,不过只能给你个思路,先把文件内容读入流然后在流里读出来,在读出前你要生成一map<单词,次数>,然后,在解决你的问题就可以了,自己做吧.呵呵
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
^^^^^6
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询