【问题标题】:How to validate user input for this Java code?如何验证此 Java 代码的用户输入?
【发布时间】:2015-11-01 19:04:40
【问题描述】:

我想要验证:仅输入数字,小时必须

 int total; //Total Minutes
    String time; //Input from keyboard


    final Scanner T = new Scanner(System.in);

    System.out.print("Enter the time in HH:MM :");
    time = T.next();

    //Get the value before the ':'
    int hour = Integer.parseInt(time.substring(0, time.indexOf(':')));


    //Get the value after the ':'
    int minute =Integer.parseInt (time.substring((time.indexOf(':') + 1), time.length()));


    //Formula of the calculation
    total = hour * 60 + minute;


    //Display the final value
    System.out.println( time +" is " + total + " minutes.");

【问题讨论】:

标签: java validation input


【解决方案1】:

为了验证,使用正则表达式:

time.matches( "(?:[01][0-9]|2[0-3]):[0-5][0-9]" )

要转换,只需使用

int hour = Integer.parseInt(time.substring(0, 2));
int minute = Integer.parseInt(time.substring(3));

【讨论】:

    【解决方案2】:

    要遵循您的格式,用户必须在第三个字符位置charAt(2) 处输入带有“:”的时间,并且其余字符(在索引 0、1、3 和 4 处)必须是数字。一个快速实施且易于学习的解决方案是使用正则表达式:http://www.regular-expressions.info/tutorial.html

    不过,我将向您展示一个使用您的代码作为模板的更易于理解的解决方案:

    1. 验证长度为5
    2. 验证时间是否正确
    3. 验证分钟数是否正确
    4. 如果有任何错误,请用户重新输入值

    代码如下:

        int total; // Total Minutes
        String time; // Input from keyboard
    
        final Scanner T = new Scanner(System.in);
    
        System.out.print("Enter the time in HH:MM :");
        time = T.next();
    
        boolean correctFormat = false;
        int hour = 0, minute = 0;
    
        while (!correctFormat) {
            correctFormat = true;
    
            if (time.length() != 5)
                correctFormat = false;
            else {
    
                // Get the value before the ':'
                hour = Integer.parseInt(time.substring(0, 2));
                if (hour >= 24 || hour < 0)
                    correctFormat = false;
    
                // Get the value after the ':'
                minute = Integer.parseInt(time.substring(3, 5));
                if (minute >= 60 || minute < 0)
                    correctFormat = false;
            }
            if (!correctFormat) {
                System.out.print("Pleaase follow the format! Enter the time in HH:MM :");
                time = T.next();
            }
    
        }
    
        // Formula of the calculation
        total = hour * 60 + minute;
    
        // Display the final value
        System.out.println(time + " is " + total + " minutes.");
    

    此外,如果您想让代码更加健壮,您可以通过捕获将由Integer.parseInt() 方法抛出的“NumberFormatException”来检查用户是否在“:”之前和之后输入了数字如果参数不是实际数字。您可以通过将上面的代码编辑如下来做到这一点:

        int total; // Total Minutes
        String time; // Input from keyboard
    
        final Scanner T = new Scanner(System.in);
    
        System.out.print("Enter the time in HH:MM :");
        time = T.next();
    
        boolean correctFormat = false;
        int hour = 0, minute = 0;
    
        while (!correctFormat) {
            correctFormat = true;
    
            if (time.length() != 5)
                correctFormat = false;
            else {
                try {
                    hour = Integer.parseInt(time.substring(0, 2));
                    minute = Integer.parseInt(time.substring(3, 5));
                } catch (NumberFormatException e) {
                    correctFormat = false;
                }
                if (correctFormat) {
                    if (hour >= 24 || hour < 0)
                        correctFormat = false;
    
                    if (minute >= 60 || minute < 0)
                        correctFormat = false;
                }
            }
            if (!correctFormat) {
                System.out.print("Pleaase follow the format! Enter the time in HH:MM :");
                time = T.next();
            }
        }
    
        // Formula of the calculation
        total = hour * 60 + minute;
    
        // Display the final value
        System.out.println(time + " is " + total + " minutes.");
    

    【讨论】:

    • 这并不是真正的验证,因为您的代码由于输入无效而遇到异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多