利用java小程序对文本内的单词个数进行计数

题目

利用一个java小程序对文本内的单词进行计数,有以下要求:
1.以字母序进行输出
2.以小写形式输出

输入:

The quick brown fox
Hopefully245this—is a quick13947
task&&#%*for you to complete.
But maybe the tASk 098234 will be less
…quicK.
the the the the the the the the the the

输出:

1 a
1 be
1 brown
1 but
1 complete
1 for
1 fox
1 hopefully
1 is
1 less
1 maybe
3 quick
2 task
12 the
1 this
1 to
1 will
1 you

解法

程序可分为几个部分,文本读入,单词处理,以及计数输出。文本读入利用普通输入流来完成,单词处理使用正则表达式对文本进行处理,之后替代,计数输出将表进行排序然后构建哈希表进行输出,值得学习的是哈希表的输出方式,在输出过程中用到了iterator和Map.entry。另外值得标记一下的是,HashMap是无序的,LinkedHashMap才是有序的,不过在本题情况下使用TreeMap貌似是个更好的选择

实现代码如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.NoSuchFileException;
import java.util.*;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{
	public static void main(String[] args) throws IOException{
		ArrayList<String> AL = new ArrayList<String>();
		try {
			FileInputStream IS = new FileInputStream("workshop1.txt");
			Scanner S = new Scanner(IS);
			while(S.hasNextLine()){ 
				StringTokenizer st = new StringTokenizer(StringFunc(S.nextLine()));
				while(st.hasMoreTokens()) {
					AL.add(st.nextToken());
				}
			}
			IS.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashFunc(AL);
	}
	
	//handle the string
	public static String StringFunc(String Str) {
		Str = Str.toLowerCase();
		Str = Pattern.compile("[^A-Za-z]+").matcher(Str).replaceAll(" ");
		return Str;
	}
	
	//put elements in a hashtable and count how many times they appear
	public static void HashFunc(ArrayList<String> AL) {
		HashMap<String, Integer> Hmap = new LinkedHashMap<>();
		Collections.sort(AL);
		for (String temp : AL) {
			Integer count = Hmap.get(temp);
			Hmap.put(temp, (count == null) ? 1 : count + 1);
		}
		Iterator iter = Hmap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();
			System.out.println(val + " " + key);
		}	
	}
}

更新一下,利用TreeMap实现上述功能,代码如下:

public class Test{
	public static void main(String[] args) throws IOException{
		TreeMap<String, Integer> Hmap = new TreeMap<String, Integer>();
		try {
			FileInputStream IS = new FileInputStream("workshop1.txt");
			Scanner S = new Scanner(IS);
			while(S.hasNextLine()){ 
				StringTokenizer st = new StringTokenizer(StringFunc(S.nextLine()));
				while(st.hasMoreTokens()) {
					String temp = st.nextToken();
					Integer count = Hmap.get(temp);
					Hmap.put(temp, (count == null) ? 1 : count + 1);
				}
			}
			IS.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HashFunc(Hmap);
	}
	
	//handle the string
	public static String StringFunc(String Str) {
		Str = Str.toLowerCase();
		Str = Pattern.compile("[^A-Za-z]+").matcher(Str).replaceAll(" ");
		return Str;
	}
	
	//put elements in a hashtable and count how many times they appear
	public static void HashFunc(TreeMap<String, Integer> Hmap) {
		Iterator iter = Hmap.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			Object key = entry.getKey();
			Object val = entry.getValue();
			System.out.println(val + " " + key);
		}	
	}
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值