统计一篇英文中每个单词出现的次数

/*功能:统计一篇英文中每个单词出现的次数
 *实现:用HashMap处理统计,用StringBuffer处理输出
 *Date:2014-5-14 深夜
 *Author:何龙
 *QQ:471628912
 */
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.Iterator;

public class StringTokenPro extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel l1, l2;
	private JPanel p1, p2;
	private JTextArea input, output;
	private JButton b1, b2;
	private JScrollPane sc1, sc2;
	private Map map;

	public StringTokenPro() {
		super("【何龙作品】  QQ:471628912");// 设置标题
		this.setSize(600, 400); // 设置窗体大小
		this.setLocation(100, 100); // 设置窗体位置
		this.setResizable(false);// 不可更改窗口大小

		// 对底层容器进行布局,注意先创建再使用(习惯性用到谁就new谁)
		this.setLayout(new GridLayout(1, 2));
		p1 = new JPanel();
		p2 = new JPanel();
		this.add(p1);
		this.add(p2);

		// // 对P1进行布局(null)
		p1.setLayout(null);
		l1 = new JLabel("输入一篇文章:");
		l1.setFont(new Font("Kaiti", Font.BOLD, 25));
		l1.setBounds(10, 10, 300, 40);
		input = new JTextArea();
		sc1 = new JScrollPane(input);
		sc1.setBounds(10, 50, 250, 230);
		b1 = new JButton("解析");
		b1.setFont(new Font("Kaiti", Font.BOLD, 25));
		b1.setBounds(10, 300, 100, 40);

		p1.add(l1);
		p1.add(sc1);
		p1.add(b1);
		b1.addActionListener(this);// 监听b1

		// 对p2进行布局(null)
		p2.setLayout(null);
		l2 = new JLabel("解析后结果:");
		l2.setFont(new Font("Kaiti", Font.BOLD, 24));
		l2.setBounds(10, 10, 300, 40);
		output = new JTextArea();
		output.setEditable(false);
		sc2 = new JScrollPane(output);
		sc2.setBounds(10, 50, 250, 230);
		b2 = new JButton("生成报告");
		b2.setFont(new Font("Kaiti", Font.BOLD, 22));
		b2.setBounds(10, 300, 100, 40);

		p2.add(l2);
		p2.add(sc2);
		p2.add(b2);
		b2.addActionListener(this);// 监听b2

	}

	// 事件儿监听
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) {
			map = new HashMap();
			// 注意每次点击“按钮时”,都必须创建一个新的HashMap,
			// 不能在构造方法里创建Map,否者下次点击按钮时会用到上次创建的Map,这样会重叠结果

			createMap(); // 生成HashMap
			output.setText(createOutput());// 处理输出显示,createOutput()返回字符串。
		}
		if (e.getSource() == b2) {
			output.setText("暂时不作IO处理!!!\n\n" + "谢谢合作!!!");
		}
	}

	// 创建HashMap();
	public void createMap() {
		StringTokenizer tokens = new StringTokenizer(input.getText(),
				", ?.!:\"\'\n\r\t\f");// 注意定界符应该包括空格
		while (tokens.hasMoreElements()) {
			String word = tokens.nextToken().toLowerCase(); // 小写处理

			if (map.containsKey(word)) {
				Integer count = (Integer) map.get(word);
				map.put(word, new Integer(count.intValue() + 1));
			} else {
				map.put(word, new Integer(1));
			}
		}
	}

	// 将HashMap字符串化
	public String createOutput() {
		StringBuffer output = new StringBuffer("统计每个单词出现的频率:\n");
		Iterator keys = map.keySet().iterator();
		while (keys.hasNext()) {
			Object currentKey = keys.next();
			output.append(currentKey + "\t" + map.get(currentKey) + "\n");
		}
		output.append("\n\n出现不同单词数目:" + map.size() + "\n");
		output.append("isEmpty:" + map.isEmpty() + "\n");// 输出map空,表示已遍历结束

		return output.toString();// output是StringBuffer,必须在后面显示调用toString();

	}

	public static void main(String args[]) {
		StringTokenPro tt = new StringTokenPro();
		tt.setDefaultCloseOperation(EXIT_ON_CLOSE);
		tt.setVisible(true);
	}
}

测试结果:

统计一篇英文中每个单词出现的次数_第1张图片

你可能感兴趣的:(JAVA,SE/WEB)