A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马灬小寒 初级黑马   /  2014-4-26 19:19  /  1266 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

输入密码   要求密码长度大于6并且包含数字、大写字母、小写字母

12 个回复

倒序浏览
符合你这个要求的应该是正则表达式。
具体方式是先记录键盘输入(scanner功能或者数据流读System.in)
然后把录入的字符串和正则表达式做equals运算。
正则表达式表示数字,大写字母,小写字母的话,可以用[0-9,a-z,A-Z]
要求长度大于6的话,可以写成[0-9,a-z,A-Z]{6,}
关于正则表达式的详细解释,你可以去http://www.jb51.net/tools/zhengze.html这个网站看下。
回复 使用道具 举报
kuroro自走核炮 发表于 2014-4-26 20:57
符合你这个要求的应该是正则表达式。
具体方式是先记录键盘输入(scanner功能或者数据流读System.in)
然后 ...

equals?   matches 吧
回复 使用道具 举报
希望能帮到你
  1. import java.util.*;

  2. public class Tank_2 {

  3.         public static void main(String[] args) {
  4.                 System.out.println("请输入6位以上的密码,必须包含数字、大写字母以及小写字母:");
  5.                 Scanner scanner = new Scanner(System.in);
  6.                 while(true){String password = scanner.next();
  7.                 try{
  8.                 if(! checkPassword(password)){
  9.                         UnSafePasswordException();
  10.                         }
  11.                         }catch(Exception e){}
  12.                 }
  13.                
  14.         }
  15.                
  16. private static void UnSafePasswordException() {
  17.                 System.out.println("输入有误密码必须6位以上,同时包含数字、大写字母以及小写字母");               
  18.         }


  19.        
  20.         public static boolean checkPassword(String password){
  21.                 boolean flag = false;
  22.                 boolean haveNumber = false;
  23.                 boolean haveUp = false;
  24.                 boolean haveLow = false;
  25.                 if(password.length() < 6){
  26.                         return flag;
  27.                 }else{
  28.                         for(int i=0;i<password.length();i++){
  29.                                 char c = password.charAt(i);
  30.                                 int x = c - '0';
  31.                                 if(x >= 0 && x <= 9){
  32.                                         haveNumber = true;
  33.                                 }
  34.                                 if(c >= 'A' && c <= 'Z'){
  35.                                         haveUp = true;
  36.                                 }
  37.                                 if(c >= 'a' && c <= 'z'){
  38.                                         haveLow = true;
  39.                                 }
  40.                         }
  41.                         if(haveNumber && haveUp && haveLow){
  42.                                 flag = true;
  43.                         }
  44.                 }
  45.                 return flag;
  46.         }

  47. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报
字符串验证用正则表达式比较好,自己写比较麻烦。
看你的要求应该不是必须包含大小写字母和数字,我是这样理解的,只有大于6位,组成可以是字母+数字,也可以是纯字母或纯数字。下面我写的不知道是不是符合你的要求。
  1. import java.util.*;
  2. public class Test
  3.         {
  4.                 public static void main(String[] ages)
  5.                         {
  6.                                 String s = null;
  7.                                 Scanner sc=new Scanner(System.in);
  8.                                 s=sc.next();
  9.                                 if (s.matches("^[A-Za-z0-9]{6,}+$"))
  10.                                         {
  11.                                                 System.out.println("输入正确");
  12.                                         }else {
  13.                                                 System.out.println("输入不合法");
  14.                                         }
  15.                         }
  16.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

回复 使用道具 举报 0 1
包含的意思是 只包含么? 其他字符可不可以出现?
回复 使用道具 举报
用正则表达式啊 [0-9a-zA-Z]{6,}
用这个匹配就可以了
回复 使用道具 举报
本帖最后由 曲佳奇 于 2014-4-26 21:38 编辑
  1. import java.util.Scanner;

  2. class Test {
  3.         public static void main(String[] args)

  4.         {
  5.                 Scanner sc = new Scanner(System.in);

  6.                 String str = sc.next();

  7.                 if(checkStr(str)){
  8.                         System.out.println("验证通过");
  9.                 }else{
  10.                         System.out.println("验证不通过");
  11.                 }
  12.         }
  13.         public static boolean checkStr(String testStr) {
  14.                 String regex1 = ".*[0-9]+.*";
  15.                 String regex2 = ".*[A-Z]+.*";
  16.                 String regex3 = ".*[a-z]+.*";

  17.                 if (testStr.length() < 6) {
  18.                         return false;
  19.                 }

  20.                 if (testStr.matches(regex1) && testStr.matches(regex2)
  21.                                 && testStr.matches(regex3)) {
  22.                         return true;
  23.                 } else {
  24.                         return false;
  25.                 }
  26.         }
  27. }
复制代码


我认为楼主说的包含大小写字母数字应该是 三种都包含..
z1342802487的回答 我感觉有些问题

回复 使用道具 举报
itpower 发表于 2014-4-26 21:23
用正则表达式啊 [0-9a-zA-Z]{6,}
用这个匹配就可以了

楼主的意思应该是三种都包含吧...
回复 使用道具 举报
import java.util.Scanner;

public class CheckPassword {
        public static void main(String[] args){
                //获得输入值
                System.out.println("请输入密码:");
                Scanner scanner = new Scanner(System.in);
                String password = scanner.next();
               
                String regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{7,}$";
                boolean res = password.matches(regex);
                if(res == true){
                        System.out.println("密码格式正确");
                }else{
                        System.out.println("密码格式错误");
                }
        }
}
回复 使用道具 举报
曲佳奇 发表于 2014-4-26 21:13
equals?   matches 吧

额。我弄错了……
回复 使用道具 举报
曲佳奇 发表于 2014-4-26 21:40
楼主的意思应该是三种都包含吧...

是的.....
回复 使用道具 举报
爱翚 发表于 2014-4-26 21:55
import java.util.Scanner;

public class CheckPassword {

我这个实现的是密码必须要7位以上并且密码中必须要包含数字以及字母的大写和小写,这是强密码的校验.
好辛苦才做出来的,希望能够给技术分.

谢谢.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马