有个a.txt文件 里面是一篇文章,
要求读取a.txt,统计出文章里面各个单词在文章中出现的次数。
然后以GUI形式输出。输入文件的路径,点击确认事件 ,
输出各个单词 和次数。不要都写在一个class里面哦~
我大概画的如下图:
http://www.krinfo.net/a.jpg还是新手 对于写个完整的code还很困难 只是知道大概怎么实现
所以高分请高手花一点点事件帮帮忙
要求读取a.txt,统计出文章里面各个单词在文章中出现的次数。
然后以GUI形式输出。输入文件的路径,点击确认事件 ,
输出各个单词 和次数。不要都写在一个class里面哦~
我大概画的如下图:
http://www.krinfo.net/a.jpg还是新手 对于写个完整的code还很困难 只是知道大概怎么实现
所以高分请高手花一点点事件帮帮忙
哦他就是要求 把GUI的class 和统计单词的 不要放在一个class里面 呵呵
高手你来了 我好高兴
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args)
{
Map<String,Integer> map=new HashMap<String,Integer>();
//字符串就相当于你读的文件了,最后将读取的单词和个数放在一个map中
String str="I love you, do you love me? oh,you don't love me";
String[] strs=str.split("\\s|,|\\?|\\.");
for(String key:strs){
if(!"".equals(key)){
if(map.isEmpty()||!map.containsKey(key))
map.put(key,1);
else
map.put(key,map.get(key)+1);
}
}
System.out.print(map);
}
}
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestFrame {
public static void main(String[] args) {
new TaskFrame();
}
}
class TaskFrame extends Frame{
private JFileChooser jf;
private JTable jt;
private TextField tf;
private Button chooseb;
private Button configb;
private String path;
private JScrollPane jsp;
String[][] datas;
private Panel p=new Panel();
TaskFrame(){
super("统计单词");
this.setSize(300,500);
this.setLocation(300,100);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf=new TextField(20);
chooseb=new Button("打开");
chooseb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jf=new JFileChooser();
jf.showOpenDialog(TaskFrame.this);
path=jf.getSelectedFile().getPath();
tf.setText(path);
}
});
configb=new Button("确认");
configb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
caculate();
} catch (IOException e1) {
e1.printStackTrace();
}
String[] title={"单词","数量"};
jt=new JTable(datas,title);
jt.setEnabled(false);
jsp=new JScrollPane(jt);
TaskFrame.this.add(jsp,BorderLayout.CENTER);
TaskFrame.this.validate();
}
});
p.add(tf);
p.add(chooseb);
p.add(configb);
this.add(p,BorderLayout.NORTH);
this.setVisible(true);
}
private void caculate()throws IOException{
if(path==null||path.trim().equals("")){return;}
BufferedReader br=new BufferedReader(new FileReader(path));
String word="";
HashMap hm=new HashMap<String, Integer>();
while((word=br.readLine())!=null){
word=word.trim();
String[] words=word.split("\\s+");
for(int i=0;i<words.length;i++){
String cword=words[i];
if(hm.get(cword)==null){
hm.put(cword, 1);
}else{
int count=(Integer)hm.get(cword)+1;
hm.put(cword,count);
}
}
}
br.close();
Set set=hm.keySet();
int i=0;
datas=new String[set.size()][2];
for(Object ss:set){
datas[i][0]=String.valueOf(ss);
datas[i][1]=String.valueOf(hm.get(ss));
i++;
}
}
}
import java.io.File;
import java.util.*;
import javax.swing.*;
public class CalcNumberUI extends JPanel{
static private final String newline = "\n";
//打开确定按钮
JButton openButton,okButton;
JTextField jtf;
//结果文本框
JTextArea result;
//选择的文件
File file ;
JFileChooser fc;
Map<String,Integer> map ;
public CalcNumberUI(){
super(new BorderLayout());
map = new HashMap<String,Integer>();
result = new JTextArea(15,50);
result.setMargin(new Insets(5,5,5,5));
//文本框不允许编辑
result.setEditable(false);
JScrollPane resultScrollPane = new JScrollPane(result);
//建立监听器对象
MyActionListener myListener = new MyActionListener();
fc = new JFileChooser();
jtf = new JTextField(20);
openButton = new JButton("选择文件");
openButton.addActionListener(myListener);
okButton = new JButton("确定");
okButton.addActionListener(myListener);
JPanel buttonPanel = new JPanel();
buttonPanel.add(jtf);
buttonPanel.add(openButton);
buttonPanel.add(okButton); add(buttonPanel,BorderLayout.PAGE_START);
add(resultScrollPane,BorderLayout.CENTER);
}
//创建显示界面
private static void createAndShowGUI(){
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SwingFileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new CalcNumberUI();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setBounds(400,300,600,800);
frame.pack();
frame.setVisible(true);
} public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
createAndShowGUI();
}
});
}
//事件监听类
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == openButton){
int returnVal = fc.showOpenDialog(CalcNumberUI.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
jtf.setText(file.getAbsolutePath());
}
}else
if(e.getSource() == okButton){
map = CalcNumber.getWordNumber(file);
result.append("统计结果:"+newline);
Set<String> keySet = map.keySet();
for(String str: keySet){
result.append("单词\t"+str + "\t共出现\t"+ map.get(str)+"\t次"+newline);
}
}
}
}
}
单词统计类import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class CalcNumber{
/**
* 根据传入的文件,计算单词出现的次数
* @param file 存放单词的文件
* @return
*/
public static Map<String,Integer> getWordNumber(File file){
Map<String,Integer> map = new HashMap<String,Integer>();
try{
BufferedReader br = new BufferedReader(new FileReader(file));
String str = null;
while((str = br.readLine()) != null){
String[] tmp = str.trim().split(" ");
for(String strTmp : tmp){
if("".equals(strTmp.trim()))
continue;
if(map.get(strTmp) != null){
map.put(strTmp,map.get(strTmp)+1);
}else {
map.put(strTmp,1);
}
}
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return map;
}
}
要是一行中像:That's ok! ok后面一个“!”分割的话,岂不是一个新的String了?