现需要统计若干段文字(英文)中的不同单词数量。
如果不同的单词数量不超过10个,则将所有单词输出(按字母顺序),否则输出前10个单词。
注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。
注3:单词大小写敏感,即'word'与'WORD'是两个不同的单词 。
输入说明
若干行英文,最后以!!!!!
为结束。
输出说明
不同单词数量。 然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。
输入样例
Failure is probably the fortification in your pole
It is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
when you Are wondering whether new money it has laid
background Because of you, then at the heart of the
most lax alert and most low awareness and left it
godsend failed
!!!!!
输出样例
49
Are
Because
Failure
It
a
alert
and
are
as
at
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
Set<String> text=new HashSet<String>();//开始先把单词输入到Set集合中
String a;
while(!(a=in.next()).equals("!!!!!"))
{
text.add(a);
}
System.out.println(text.size());//输出不重复的单词数量,即Set集合中存储的所有元素的数量
String[] b = text.toArray(new String[text.size()-1]);//将Set中的元素返回到数组b中
ArrayList<String> text2=new ArrayList<String>();
for(int i=0;i<text.size();i++) text2.add(b[i]);//为了方便排序,再将数组b中的元素一一存到ArrayList集合中
text2.sort(null);
for(int i=0;i<10;i++)//将ArrayList集合中排好序的元素输出前十个
{
System.out.println(text2.get(i));
}
}
}