等我,就写出来
import java.io.*;
import java.util.HashMap;
import java.util.Map.Entry;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\text.txt"));//我就不写有界面的了,这个参数就是你英文句子文件所在位置
String sentence = null;
HashMap
while((sentence = br.readLine())!=null){
sentence = sentence.replaceAll("[\\pP'$']", "");
String[] words = sentence.split(" ");
for(String word:words){
if(map.get(word)==null)
map.put(word, 1);
else map.put(word, map.get(word)+1);
}
}
java.util.Iterator
while(iter.hasNext()){
Entry entry = (Entry) iter.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//Java编程:输入一段英文:
//1、统计一共有多少个单词;
//2、每个单词出现的次数;
//3、按出现次数升或降序排列
package sentencecountword;
import java.util.*;
/**
*
* @author dell
*/
public class SentenceCountWord {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in = new Scanner(System.in);
System.out.print("plese input a English sentence(输入一个句子): ");
String str = in.nextLine();
String[] word = str.split(" ");
int wordNum = word.length;
int i;
for (i = 0; i < word.length; i++) {
// System.out.println(word[i]); //该行分别输出各个单词。
}
String str1 = str.toUpperCase(); //转化成大写 System.out.println("将该句子转化成大写后为: " + str1);
String str2 = str.toLowerCase(); //转化成小写 System.out.println("将该句子转化成大写后为: " + str2);
System.out.println("这个句子中的单词个数有:" + i + "个。");
for (i = 0; i < word.length; i++) {
int sum = 0;
for (int j = 0; j < word.length; j++) {
if (word[i].equals(word[j])) { //判断两个字符串是否相等用equals, 不能用“==”
sum++;
}
}
System.out.println(word[i] + " 出现的次数为:" + sum + "次。");
}
}
}
给你个提示,用hash。