【问题标题】:How to validate string if the first 3 positions are letters如果前 3 个位置是字母,如何验证字符串
【发布时间】:2020-03-19 22:36:33
【问题描述】:

我正在尝试验证用户输入的字符串。用户必须输入一个 7 个字符的字符串;字符串前 3 个字符必须是字母,后 4 个字符必须是数字。

我写了这段代码(作为一个方法),但由于某种原因,它接受第一个字符作为数字(它应该是一个字母),其余的都是数字。例如:

Please enter word : **1gy2345**

这将根据需要进入循环,然后进入 main 中的下一个方法。

如果用户输入一个长度大于 7 的单词,它会要求他输入一个有效的单词。

例如:

Please enter word : **bob12345**
The word entered is invalid. Please enter a word beginning with 3 letters and ending with 4 numbers ( The word must be 7 characters long ).

这是我的代码:

public static final String solicitationMessage = " Please enter word ";

    public static final String errorMessage = " The word entered is invalid. Please enter a word beginning with 3 letters and ending with 4 numbers ( The word must be 7 characters long ).

    public static final int lengthOfString = 7;

    public static String validateString(String solicitationMessage, String errorMessage, int lengthOfString) {

        System.out.print(solicitationMessage);

        String word = keyboard.nextLine();

        while (!( word.length() == lengthOfString )) {
            if (((word.charAt(0) <= 'a' || word.charAt(0) >= 'z') || (word.charAt(1) <= 'a' || word.charAt(1) >= 'z')
                    || (word.charAt(2) <= 'a' || word.charAt(2) >= 'z'))) {
                System.out.print(errorMessage);
                System.out.print(solicitationMessage);
                word = keyboard.nextLine();
            }
        }
        return word;
    }

但是,如果我输入一个高于 7 个限制字符的字符串,它会再次要求我输入一个有效的字符串,就像它应该做的那样。

不允许使用正则表达式。

有什么帮助吗?

【问题讨论】:

  • 你只输入长度不是7的循环,所以任何长度为7的字符串都被认为是有效的,例如!@#$%^&amp; 会起作用。
  • 另外,您可能希望将 &lt;= 比较更改为 &lt; - 因为您希望将 az 视为字母。

标签: java methods


【解决方案1】:

这对正则表达式来说很容易

[a-zA-Z]{3}[0-9]{4}

部分测试用例:http://www.regexplanet.com/cookbook/ahJzfnJlZ2V4cGxhbmV0LWhyZHNyEwsSBlJlY2lwZRiAgICi0ZuYCgw/index.html

所以它可能是这样的

Pattern pattern=Pattern.compile("[a-zA-Z]{3}[0-9]{4}");
String line;
while(true){
   line= keyboard.nextLine();
   if(pattern.matcher(line).matches()){
     break;
}
}

【讨论】:

  • 我不能使用正则表达式。
  • @ForzaTekk 密钥信息省略。请重新编辑您的问题以解释允许的内容,以便其他人无法提供可行但不可接受的解决方案。
  • @ForzaTekk 为什么不能使用正则表达式?对我来说似乎是一个奇怪的限制。
  • 我们还没有在 Java 中学习它。但是,我在 Ubuntu 上的另一个课程(计算机系统管理/管理)中使用了正则表达式。
  • 我们还没有学过Java。所以你有学习限制,你现在不能学会使用正确的工具来完成这项工作??奇怪的学校.....
【解决方案2】:

由于这是一个作业,我只会给你伪代码。您尝试弄清楚如何实现它:p

boolean function validateString(inputString):
    check if inputStrings length is exactly 7:
        if not then return false;
    loop through inputString:
        if the current index is less than 3 and current character is within 'a' - 'z' or within 'A' - 'Z' // check if current character is a letter
           return false;
        else if current index is greater than 2 and current character is not within '0' - '9' // check if current character is not a digit
           return false;
    end loop
    return true
end function

然后只需在您的 main 方法中调用该函数并打印必要的错误消息。

【讨论】:

    【解决方案3】:

    试试这个。使用 Character 类来测试字符。

            if (!(Character.isLetter(word.charAt(0)) &&
                 Character.isLetter(word.charAt(1)) &&
                 Character.isLetter(word.charAt(2)))) {
                     System.out.print(errorMessage);
                     System.out.print(solicitationMessage);
                     word = keyboard.nextLine();
            }
    
    
    

    【讨论】:

    • 它不起作用。我将字符串输入为 1hi1234 并对其进行验证。
    • 这里的问题是解决方案缺少一个 NOT - 如果满足验证要求,它会打印错误,而不是不满足。
    • 我更多地考虑验证这个词而不是抓住一个坏词。过错。我根据 Dawood 的 建议修复了代码
    【解决方案4】:

    也许这会有所帮助。

    public static String validateString(String solicitationMessage, String errorMessage, int lengthOfString) {
    
        System.out.print(solicitationMessage);
    
        String word = "";
        boolean flag = false;
        while (true) {
            Scanner sc = new Scanner(System.in);
            word = sc.nextLine();
            if(word.length() == lengthOfString){
                for(int i=0;i<word.length();i++){
                    int ascii = (int)word.charAt(i);
                    if(i<3 && (ascii > 64 && ascii < 123)){
                        flag = true;
                    }else if(i>2 && (ascii > 47 && ascii < 58)){
                        flag = true;
                    }else{
                        flag = false;
                        System.out.println(errorMessage);
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }else{
                System.out.println(errorMessage);
            }
        }
        return word;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      相关资源
      最近更新 更多