2015-11-07 31 views
-1

我想確保有5個字符在什麼用戶輸入將其更改爲軍事時間和幾乎這是格式##:##(與冒號一樣),這是我的代碼的一部分,但我無法讓它工作。MIlitary時間驗證

任何幫助將是偉大的 - 謝謝。

public Time(String militaryTime) 
{ 
    //Check to make sure something was entered 
    if (militaryTime == null || !militaryTime.matches("^\\d{2}:\\d{2}$")) 
    { 
     System.out.println(
      "You must enter a valid miliary time."); 
    } 
    //Check to make sure there are 5 characters 
    else 
    { 
     //Check to make sure the colon is in the right spot 

     if (!Character.isDigit(militaryTime.charAt(2))) 
     { 
      System.out.println(militaryTime + 
       " is not a valid miliary time."); 
     } 
     //Check to make sure all other characters are digits 
     else if (!Character.isDigit(militaryTime.charAt(0))) 
     { 
      System.out.println(militaryTime + 
       " is not a valid miliary time."); 
     } 
     else if (!Character.isDigit(militaryTime.charAt(0))) 
     { 
      System.out.println(militaryTime + 
       " is not a valid miliary time."); 
     } 
     else if (!Character.isDigit(militaryTime.charAt(0))) 
     { 
      System.out.println(militaryTime + 
       " is not a valid miliary time."); 
     } 
     else if (!Character.isDigit(militaryTime.charAt(0))) 
     { 
      System.out.println(militaryTime + 
       " is not a valid miliary time."); 
     } 
     else 
     { 
      //this separates hours and minutes 
      hours = Integer.parseInt(militaryTime.substring(0,2)); 

      //validate hours and minutes are valid values 
      if(hours > 23) 
      { 
       System.out.println(militaryTime + 
        " is not a valid miliary time."); 
      } 
      else if(minutes > 59) 
      { 
       System.out.println(militaryTime + 
        " is not a valid miliary time."); 
      } 
      //convert military time to conventional time 
      //for afternoon times 
      else if (hours > 12) 
      { 
       hours = hours - 12; 
       afternoon = true; 
       System.out.println(this.toString()); 
      } 
      //account for midnight 
      else if (hours == 0) 
      { 
       hours = 12; 
       System.out.println(this.toString()); 
      } 
      //account for noon 
      else if (hours == 12) 
      { 
       afternoon = true; 
       System.out.println(this.toString()); 
      } 
      //morning times don't need converting 
      else 
      { 
       System.out.println(this.toString()); 
      } 
     } 
    } 
} 
+1

我看不出有什麼軍事與此有關。看來你正在談論一個標準的24小時時間格式。 –

+2

@a_horse_with_no_name https://en.wikipedia.org/wiki/24-hour_clock 「它在美國,講英語的加拿大和少數幾個其他國家通常被稱爲軍事時間,其中12小時的時鐘仍然占主導地位。「 –

+0

@SkinnyJ:真的嗎?聽起來很奇怪...... –

回答

3

在大多數語言最簡單的方法是使用regular expression

if (militaryTime == null || !militaryTime.matches("^\\d{2}:\\d{2}$")) { 
    System.out.println(militaryTime + " is not a valid military time."); 
} 

這不會檢查時間是0-24之間或分鐘爲0-60之間,但你代碼似乎也不在乎。

或者您可以使用Java time API

try { 
    DateTimeFormatter.ofPattern("HH:mm").parse(militaryTime) 
} 
catch (DateTimeParseException e) { 
    System.out.println(militaryTime + " is not a valid military time."); 
} 

這將驗證它是完全兼容的。

+0

我編輯和發佈的代碼,但仍然當我輸入時間爲##:##它不能正常運行?關於這個當前混亂的代碼的任何想法? – xTuckii

+0

@xTuckii現在完全不清楚你現在要問什麼。問題是如何確保'String'是格式##:##其中##是2位數字。我已經回答了這個問題。 「它運行不正常」不是問題。 –

+0

對不起,我的問題是,爲什麼即使在運行的程序中輸入##:##格式後,它仍然說這不是一個有效的軍事時間。 – xTuckii

0

試試這個代碼

public static boolean isMilitoryTmeString(String militaryTime) { 
      // Check to make sure something was entered 
      if (militaryTime == null) { 
       return false; 
      } 
      // Check to make sure there are 5 characters 
      if (militaryTime.length() != 5) { 

       return false; 
      } 

      // Storing characters into char variable 
      char hourOne = militaryTime.charAt(0); 
      char hourTwo = militaryTime.charAt(1); 
      char colon = militaryTime.charAt(2); 
      char minuteOne = militaryTime.charAt(3); 
      char minuteTwo = militaryTime.charAt(4); 

      //first position of hour must be 0 or 1 or 2 
      if (hourOne != '0' && hourOne != '1' && hourOne != '2') { 

       return false; 
      } 
      //if first position of hour is 0 or 1 then second 
      //position must be 0-9 
      if (hourOne == '0' || hourOne == '1') { 

       if (hourTwo < '0' || hourTwo > '9') { 
        return false; 
       } 

      //if hourOne equal 2 then second position must be 0-3 
      } else { 

       if (hourTwo < '0' || hourTwo > '3') { 
        return false; 
       } 
      } 
      //third position must be colon 
      if (colon != ':') { 

       return false; 
      } 
      // fourth position must be 0-5 
      if (minuteOne < '0' || minuteOne > '5') { 
       return false; 
      } 

      //fifth position must be 0-9 
      if (minuteTwo < '0' || minuteTwo > '9') { 
       return false; 
      } 
      // String is valid military time 
      return true; 
     }