Bufferedreader验证检查错误

时间:2014-06-16 15:08:30

标签: java bufferedreader

我很确定我有这个代码正常工作然后由于某种原因今天我没有改变任何东西而且它没有正常工作。当我输入1个字符时,我才能使它工作,否则它会失败。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   

System.out.println("Enter your 1-12 capitalized alphanumeric character DLN: ");

String DLN = null;
DLN = br.readLine();                                                            

    if(DLN == null || DLN.length() > 12 || DLN.length() < 1 || !DLN.matches("[A-Z0-9]")) {      
       System.out.println("You have not entered a 1-12 character DLN");
       return;
    }

1 个答案:

答案 0 :(得分:2)

您当前的正则表达式只允许一个字符。试试这个:

!DLN.matches("[A-Z0-9]+")
// ...................^

或者更好的是,将长度要求表达为正则表达式的一部分:

if(DLN == null || !DLN.matches("[A-Z0-9]{1,12}")) {      
   System.out.println("You have not entered a 1-12 character DLN");
   return;
}