【问题标题】:How to fix my Java code for validating variables?如何修复我的 Java 代码以验证变量?
【发布时间】:2015-10-26 13:00:10
【问题描述】:

我目前正在我的计算机科学课上做一个项目,我们假设验证变量的 每个 字符以查看它是否合法。如果它以数字开头,那是非法的。如果它以特殊字符开头,则它是合法的,但样式不好。如果它有一个空间,它又是非法的。我现在将发布我当前的代码:

import java.util.Scanner;

public class classOfValidation {

    public static void main(String[] args) {    
        Scanner scan = new Scanner(System.in);    
        String theVariable = null;

        System.out.println("This program checks the validity of variables");    
        System.out.println("Please enter a variable (or press 'q' to quit");

        theVariable = scan.nextLine();

        do {        
            System.out.println("The variable is illegal");    
            theVariable = scan.nextLine();    
        } while (theVariable.startsWith("[0123456789]"));

        do {    
            System.out.println("The variable is illegal");    
            theVariable = scan.nextLine();    
        } while (theVariable.contains("[ ]"));

        do {    
            System.out.println("The variable is legal, but has bad style");    
            theVariable = scan.nextLine();    
        } while (theVariable.startsWith("[!@#$%^&*]"));         
    }    
}

如果您还不能告诉我我是编程新手并且可能会感到困惑。如果您有任何建议或其他需要我解释的内容,请发表评论。谢谢大家

【问题讨论】:

  • 如果你第一次输入像“asdbasdb”这样的变量,程序会跳过第一个而输入第二个,让我使用“1234”作为变量名而不说它是错误的。您应该将代码放入一个 while(或 do...while)循环中,并使用 if 语句检查不同方面,只记住在当前循环运行期间这些 if 之一是否为真(像“isValidName”这样的布尔变量可以在这里帮助)。

标签: java char


【解决方案1】:

您可以使用单个regex 通过String#matches() 方法验证您的输入。但是对于您提供的示例,您应该使用while 循环,而不是do-while,因为在do while 的情况下,您总是在检查条件之前运行它的主体一次。所以,你最好这样做:

theVariable = scan.nextLine();

while (theVariable.startsWith("[0123456789]")) {
    System.out.println("The variable is illegal");
    theVariable = scan.nextLine();
}

while (theVariable.contains("[ ]")) {
    System.out.println("The variable is illegal");
    theVariable = scan.nextLine();
}

while (theVariable.startsWith("[!@#$%^&*]")) {
    System.out.println("The variable is legal, but has bad style");
    theVariable = scan.nextLine();
}

第二个,在您的解决方案中,您正在使用 String.startsWith() 方法并将一些正则表达式传递给它。看看javadoc 这个方法。那里说:

测试此字符串是否以指定的前缀开头。

也就是说,这个方法不支持正则表达式,只是检查字符串是否以传递的字符串开头。因此,您的条件似乎永远不会成为现实。我不认为,有人会输入[0123456789][!@#$%^&*]

另外,任何条件都检查一次,但之后用户可以修改输入,并且不会再次检查预览通过的条件。似乎,在某些情况下最好使用continuebreak 进入无限循环,例如:

 //infinit loop, until user enter the `q` or the input is correct
 while (true) {

    //read the input
    theVariable = scan.nextLine();

    //chtck, whether is `quit` command entered
    if ("q".equals(theVariable)) {
        break;
    }

    //if string starts with digit or contains some whitespaces
    //then print alert and let the user to 
    //modify the input in a new iteration
    if (theVariable.matches("^\d+.*|.*\s+.*")) {
        System.out.println("The variable is illegal");
        continue;
    }

    //if string contains some special characters print alert 
    //and let the user to modify the input in a new iteration
    if (theVariable.matches("^[!@#$%^&*].*")) {
        System.out.println("The variable is legal, but has bad style");
        continue;
    }

    //if all the conditions checked, then break the loop
    break;
}

【讨论】:

  • 谢谢你的例子,但是你知道为什么当我添加while语句时,每次我尝试输入任何东西,程序都会终止吗?
  • @TheNewYo 我认为,问题在于您使用的条件。您将正则表达式传递给 startsWith 方法,但它不会将参数作为正则表达式,而是作为一个简单的字符串。
  • 这解决了我的问题并保存了我的成绩,谢谢
【解决方案2】:

如果您使用正则表达式,我认为最好的方法。

Here 是如何做到这一点的答案。

【讨论】:

  • 您能否解释更多,例如我是否应该使用 if 语句,您能否提供一个示例来说明如何将其合并到我的代码中?感谢您的回答
猜你喜欢
  • 2019-05-19
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2021-01-11
  • 2013-08-25
  • 2017-03-10
  • 1970-01-01
  • 2016-03-05
相关资源
最近更新 更多