java字符测试仪(大写A-Z,小写A-Z,数字0-9,其他)
我正在尝试制作一个程序,告诉我在参数中输入的字符是大写还是小写,还是0到9之间的数字或其他!我的代码中有错误:java字符测试仪(大写A-Z,小写A-Z,数字0-9,其他),java,uppercase,lowercase,digit,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(in
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");
}
}
}
更改输入的类型
String input;// Change char to String
if (input.matches("[a-zA-Z]+")) // Remove ^ and $
// String.matches don't need symbol ^ $
要测试
char
您不需要String#匹配项
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");
}
尝试:
如果您的输入实际上是一个字符,那么您不需要正则表达式,只需要一组测试
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 :/");
}
正则表达式组解决方案:
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");
}
您可以将字符串转换为字符数组,然后使用ascii值将单个字符转换为如下所示:-
公共类CharsTester{
公共静态void main(字符串[]args){
字符串输入=“ASD123asd_-”;
char temp[]=input.toCharArray();
对于(int i=0;i47&&temp[i]64&&temp[i]96&&temp[i]有什么错误?您提供的信息越多,我们就可以帮助您更快更好。Stacktrace或excaption?您不能调用方法matches()
在诸如char的基本类型上,因为它不是一个对象。那么我应该使用什么方法bro?测试的输入/数据总是一个字符吗?错误:类型字符中的方法isLowerCase(char)不适用于参数(String)@johnmarkaad Just docharacter.isLowerCase(input.charAt(0))
@johnmarkaad,你想测试什么?如果你想测试char而不是why匹配?编辑并更正了它-我想你想解析所有程序参数。只要运行我的代码,如果这是你想要的,请将其标记为正确答案。如果你想要不同的东西,请更具体地说明你需要什么。谢谢你的兄弟,你得到了一个point、 有其他错误,但是,我现在将尝试找出它。(简单错误)
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");
}
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");
}
}
}