public static void main (String [] args){
Listlist = new ArrayList ();
Scanner scan = new Scanner(System.in);
System.out.println("Please input the first name: ");
list.add(getName(scan.next(),"first"));
System.out.println("Please input the second name: ");
list.add(getName(scan.next(),"second"));
System.out.println("Please input the third name: ");
list.add(getName(scan.next(),"third"));
scan.close();
//剔除为null的对象(因为不合法的名称被设置为null)。
//此处为何用ite迭代删除?原因参考http://blog.csdn.net/longyulu/article/details/8315068
Iteratorite = list.iterator();
while(ite.hasNext()){
String s = ite.next();
if(s == null){
ite.remove();
}
}
//排序
Collections.sort(list);
//输出最终结果
System.out.print("The names are :");
for (String s : list) {
System.out.print("\""+s+"\" ");
}
}
private static String getName(String name , String index) {
String regEx="^[A-Za-z]+$"; //纯字母正则表达式
Pattern pat=Pattern.compile(regEx);
if(pat.matcher(name).matches()){
//全部转为小写
name = name.toLowerCase();
//提取第一个字母并转换大写
String first = (name.charAt(0)+"").toUpperCase();
name = first+name.substring(1);
System.out.println(name+" is the "+index+" name.");
}else{
name = null;
System.out.println("Error: The "+index +" name was not accepted.");
}
return name;
}
//控制台结果:
Please input the first name:
tsKd
Tskd is the first name.
Please input the second name:
ukf90
Error: The second name was not accepted.
Please input the third name:
admin
Admin is the third name.
The names are :"Admin" "Tskd"
这个很简单,不过我现在没那么多时间,不急的话可以晚上帮你解决。
同意楼上.作业还是自己做比较好,掌握得比较牢.
作业要自己做