Character.isLetterOrDigit(char c)的使用和matches方法的使用

/*
2.用户输入密码,要求密码满足的条件是:长度大于6且包含数字、大写字母和小写字母,如果不满足条件,则抛出UnSafePasswordException自定义异常类对象。
Character.isLetterOrDigit(char c)的使用
*/


import java.util.*;
class UnSafePasswordException extends Exception{
UnSafePasswordException(String s){
super(s);
}
public void printMsg(){
System.out.println("exception="+this.getMessage());
this.printStackTrace();
System.exit(0);
}


}
class PasswordDemo{
String s;
public void num() throws UnSafePasswordException{
System.out.print("请输入密码:");
Scanner sc=new Scanner(System.in);
s=sc.nextLine();
if(s.length()>6){
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(Character.isLetterOrDigit(c)){

}else{
throw new UnSafePasswordException("密码格式有误...");

}
}
}else{
throw new UnSafePasswordException("密码长度应大于6位...");
}
System.out.println("密码符合格式...");
}
public static void run(){
try
{
new PasswordDemo().num();
}
catch (UnSafePasswordException e)
{
e.printMsg();
}


}
public static void main(String[] args) 
{
run();
}

}



/*
2.用户输入密码,要求密码满足的条件是:长度大于6且包含数字、大写字母和小写字母,如果不满足条件,则抛出UnSafePasswordException自定义异常类对象。 
matches方法的使用


*/


import java.util.*;
class UnSafePassworddException extends Exception{
UnSafePassworddException(String s){
super(s);
}
public void printMsg(){
System.out.println("exception="+this.getMessage());
this.printStackTrace();
System.exit(0);
}


}
class PasswordDemo1{
String s;
public void num() throws UnSafePassworddException{
System.out.print("请输入密码:");
Scanner sc=new Scanner(System.in);
s=sc.nextLine();
if(s.length()>6){
String ss="[0-9&a-z]{6,}";
if(s.matches(ss)){
}else{
throw new UnSafePassworddException("密码格式有误...");
}
}else{
throw new UnSafePassworddException("密码长度应大于6位...");
}
System.out.println("密码符合格式...");
}
public static void run(){
try
{
new PasswordDemo1().num();
}
catch (UnSafePassworddException e)
{
e.printMsg();
}


}
public static void main(String[] args) 
{
run();
}
}


posted on 2012-04-24 19:44  java课程设计  阅读(2895)  评论(0编辑  收藏  举报

导航