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

问题描述:

我想验证一下:仅限数字输入,小时必须是< = 24,分钟必须是< 60,用户必须在hh:mm之间输入':'符号。如何验证此Java代码的用户输入?

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

[常规匹配HH表达:MM时间格式]的可能的复制:(http://*.com/questions/7536755/regular-expression-可以通过编辑上述代码,以如下这样做for-matching-hhmm-time-format) – Thevenin

+0

你现在不会想要一个24:53的时间,是吗?那么为什么 laune

+0

对不起 Flipz

按照你的格式,用户必须输入与时间 ':' 在第三个字符位置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."); 

此外,如果你想更强大的代码,你可以检查,如果用户之前和之后的实际进入数字“:”通过捕捉一个“NumberFormatException异常'如果参数不是实际的数字,将从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."); 
+0

由于输入无效,您的代码会遇到异常,因此这并不是真正的验证。 – laune

+0

好的,赶上:) – Bimde

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

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