java中的Char Tester(大写字母A-Z,小写字母a-z,数字0-9,其他)

时间:2013-11-26 15:24:54

标签: java uppercase lowercase digit

我正在尝试创建一个程序,告诉我天气,我在参数中放入的字符是大写或小写,还是0到9或其他数字!我的代码中有错误:

public class CharsTester {


    public static void main(String[] args) {


         char input;

         if (input.matches("^[a-zA-Z]+$")) 
         {
             if (Character.isLowerCase(input))
             {
                System.out.println("lower");
             } 
             else
             { 
                 System.out.println("upper");
             }
         }
         else if (input.matches("^(0|[1-9][0-9]*)$"))
         {
             System.out.println("digit");
         }
         else
         {
             System.out.println("other");
         }
    }
}

5 个答案:

答案 0 :(得分:3)

更改输入类型

String input;// Change char to String 

if (input.matches("[a-zA-Z]+")) // Remove ^ and $
                              // String.matches don't need symbol ^ $

要测试char,您不需要String#matches

  char input=' ';

    if (Character.isLowerCase(input) || Character.isUpperCase(input)) {
        if (Character.isLowerCase(input)) {
            System.out.println("lower");
        } else {
            System.out.println("upper");
        }
    } else if (Character.isDigit(input)) {
        System.out.println("digit");
    } else {
        System.out.println("other");
    }

答案 1 :(得分:0)

尝试:

for (String arg : args) {
    if (arg.matches("^[A-Z]+$")) {
        System.out.println("uppercase");
    } else if (arg.matches("^[a-z]+$")) {
        System.out.println("lowercase");
    } else if (arg.matches("^[0-9]+$")) {
        System.out.println("digits");
    } else {
        System.out.println("other");
    }
}

答案 2 :(得分:0)

如果您的输入实际上是一个字符,那么您不需要正则表达式,只需设置一些测试。

char input = 'z';// example character

if (Character.isLowerCase(input)) {
    System.out.println("lower");
} else if (Character.isUpperCase(input)) {
    System.out.println("upper");
} else if (Character.isDigit(input)) {
    System.out.println("digit");
} else {
    System.out.println("something is not right :/");
}

答案 3 :(得分:0)

Regex分组解决方案:

Pattern p = Pattern.compile("([a-z])|([A-Z])|([\\d])");
Matcher m = p.matcher("" + x);
m.matches();
if (m.group(1)!=null)
{
  System.out.println("lower");
} 
else if (m.group(2)!=null)
{ 
  System.out.println("upper");
}  
else if (m.group(3)!=null)
{ 
  System.out.println("digit");
} else
{ 
  System.out.println("other");
}    

答案 4 :(得分:0)

you can convert String to char array, and then individual char with ascii value as below:-


public class CharsTester {


    public static void main(String[] args) {


        String input="ASD123asd_-";
        char temp[]=input.toCharArray();
        for (int i=0;i<temp.length;i++)
        {
            if(temp[i] >47 && temp[i]<58)
            {
                System.out.println(temp[i]+": is digit");
            }
            else if(temp[i] >64 && temp[i]<91)
            {
                System.out.println(temp[i]+": is CAPS char");
            }
            else if(temp[i] >96 && temp[i]<123)
            {
                System.out.println(temp[i]+": is small char");
            }
            else
                System.out.println(temp[i]+"is symbol or spetial char");

        }

    }
}