如何识别数字和字母的输入

时间:2013-11-03 07:57:53

标签: java

我修复了我的代码,以便识别出他们的4位数字以及更少或6位数字以及更高但现在我想知道它是否包含数字中的字母。

下面的代码检测字母并打印出我想要的行,只有当我输入5个字母时,我希望它能检测到它们的数字多于字母或多于数字而不是数字。

    String digit;
    String regex;
    String regex1;
    regex = "[0-9]{5}";
    regex1 = "^[a-zA-Z0-9]{5}";
    String test;
    String validLength= "5";
    char one, two, three, four, five;
    {
    System.out.println("In this game, you will have to input 5 digits.");        
    do
    {
    System.out.println("Please input 5-digits.");
    digit = console.next();
    test = digit.replaceAll("[a-zA-Z]", "");
    if (digit.matches(regex))
    {
    one = (char)digit.charAt(0);
    two = (char)digit.charAt(1);
    three = (char)digit.charAt(2);
    four = (char)digit.charAt(3);
    five = (char)digit.charAt(4);
    System.out.println((one + two + three + four + five) / 2 );
    }
    else if (test.length() > 5 || test.length() < 5)
        {
            System.out.println("You have letters in there.");
        }
    else if (digit.matches(regex1))
    {
       test = digit.replaceAll("[a-zA-Z]", "");
       System.out.println("You have letters in there.");
    }
    else 

    if (digit.length() < 5)
        {
            System.out.println("You don't have enough digits.");
        }
        else if (digit.length() > 5)
        {
            System.out.println("You have to many digits.");
        }
} while (!digit.matches(regex));

2 个答案:

答案 0 :(得分:2)

我不会在这里进入正则表达式,因为老实说,我认为在经历细节的道路之前,应该解决一些误解;另外,我不是专家,我个人认为他们更像是通行权。

无论如何,让我们从头开始,或者至少在你确定他们已经输入有效的digit时,

if (digit.matches(regex))

让我们说......

String digits = "12345";
System.out.println(getSum(digits) / 2);

其中...

public int getSum(String digits) {
  int sum = 0;
  for(int i = sum; i < digits.length(); i++) {
    sum += digits.charAt(i);
  }
  return sum;
}

与您的System.out.println((one + two + three + four + five) / 2 );相同。

  

我希望127的输出让你微笑。

走出困境,因为你没有说出“char”值的总和,你希望它将你的char s视为十进制数字。那么,这将导致7。我只猜测因为整个(char)charAt()的事情。这个 - &gt; (char)charAt()显示缺乏理解会使正则表达式的使用受到高度质疑,当然是恕我直言。

超越else if (test.length() > 5 && test.length() < 5)。这说,“如果测试的长度大于且小于5”!没有使用一些数学悖论,请告诉我这个数字。

那么,关于你的问题 -

  

但现在我想知道它是否包含字母内的字母   号。

好吧,让我们看看如何找出是否存在任何非数字可能 - 没有正则表达式,所以我们可以理解它......

public boolean containsNonDigits(String digits) {
    for(int i = 0; i < digits.length(); ++i) {
        if(Character.isDigit(digits.charAt(i))) {
            continue;
        } else {
            return true;
        }
    }
    return false;
}

这说,“如果角色是一个数字继续;一切都很好,否则是假的。”

另一个“问题” -

  

我希望它能够检测到它们的数字多于字母或更多   字母而不是数字。

是上述方法的“添加剂”,所以我将把它留给你。

答案 1 :(得分:1)

您可以使用Charachter.isDigit(char)Charachter.isLetter(char)方法。

这里的代码示例实现了您的要求:

public static void main(String[] args)
{
    System.out.println("In this game, you will have to input 5 digits.");
    int validLength = 5;
    boolean valid = false;
    Scanner console = new Scanner(System.in);
    while (!valid)
    {
        System.out.println("Please input 5-digits.");
        String digit = console.next();
        if (digit.length() != validLength)
        {
            //if length not valid, mark as not valid and return to next iteration
            valid = false;
            String message = digit.length() < validLength ? "You don't have enoght digits." : "You have to many digits.";
            System.out.println(message);
            continue;
        }
        //here digit.length = 5
        int nDigits = 0,nLetters = 0,sum = 0;
        for (int i = 0; i < digit.length(); i++)
        {
            Character ch = digit.charAt(i);
            if (Character.isDigit(ch))
            {
                nDigits++;
                sum += Integer.parseInt(ch.toString());
            }
            else if (Character.isLetter(ch)) {
                nLetters++;
            }
        }
        if (nLetters == 0           /* no letters */
                ||                  /* and */   
            nDigits == validLength  /* all chars are digits */)
        {
            System.out.println(sum/2);
            valid = true;
        }
        else{
            System.out.println("You have letters in there.");
        }
    }
}