如何验证此Java代码的用户输入?

时间:2015-11-01 19:04:41

标签: java validation input

我希望验证:仅输入数字,小时必须&lt; = 24,分钟必须<60,用户必须在hh:mm之间键入':'符号。

 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.");

2 个答案:

答案 0 :(得分:1)

要进行验证,请使用正则表达式:

INNER JOIN

要进行转换,只需使用

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

答案 1 :(得分:0)

要按照您的格式操作,用户必须输入时间&#39;:&#39;在第3个字符位置charAt(2),其余字符(在索引0,1,3和4处)必须是数字。一个非常快速实现和好学的解决方案是使用正则表达式:http://www.regular-expressions.info/tutorial.html

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

  1. 确认长度为5
  2. 验证小时是否正确
  3. 验证会议记录是否正确
  4. 如果有任何错误,请让用户重新输入值
  5. 以下是代码:

        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.");
    

    此外,如果您想让代码更加健壮,您可以检查用户是否在&#39;:&#39;之前和之后输入了数字。通过捕获&#39; NumberFormatException&#39;如果参数不是实际数字,将从Integer.parseInt()方法抛出。您可以通过编辑上面的代码来执行此操作:

        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.");