2016-09-21 83 views
-2

我一直在一個程序,告知用戶,如果日期是在這個格式爲「MM/DD/YYYY」日期驗證問題的Java程序

有效的,但我的程序不管什麼說,每日期是否有效。 有人可以幫助我,並告訴我我的問題在哪裏,如果是這樣解釋什麼是錯的。必須涉及閏年。並且必須給出我認爲我可以弄清楚的錯誤信息。

這是我的代碼,請幫助我。我是初學者所以謝謝你的批評

/** 
* Created by MacOSX on 9/18/2016. 
*/ 
import java.util.Scanner; 

public class ASS4ID1773 { 

    public static void main(String[] args) { 
    } 

    { 

     System.out.println("Enter any date in ''mm/dd/yyyy'' format. "); 
     String date = ""; 
     Scanner keyboard = new Scanner(System.in); 

     int mm = 00; 
     int dd = 00; 
     int yyyy = 0000; 
     date = keyboard.nextLine(); 
     boolean LeapYear; 
     mm = 0; 
     dd = 0; 
     yyyy = 0; 
     LeapYear = false; 
     if (yyyy % 4 == 0 && (!(yyyy % 100 == 0) || yyyy % 400 == 0)) { 
      LeapYear = true; 
     } 

     /** 
     * month restrictions. 
     */ 
     if ((mm < 12) & (mm > 1)) { 
      System.out.println("You have entered an invalid month. Please try entering a month that exists."); 
     } 

     /** 
     * Day Restriction 
     */ 
     if ((dd > 31 && dd < 1)) { 
      System.out.println("You have entered an invalid day. Please try entering a day that exists."); 
     } 

     /** 
     * Months with 31 days 
     */ 
     if ((mm == 9 & mm == 4 & mm == 6 & mm == 11) & !(dd == 31)) { 
      System.out.println("For the month you have entered, you have entered an incorrect day."); 
     } 

     /** 
     * February month 
     */ 
     if ((mm == 2 && !(dd < 29)) && LeapYear == false) { 
      System.out.println("You have entered a day that does not exist in the month of February."); 
     } 

     /** 
     * Leap Year for February but with incorrect day. 
     */ 
     if ((mm == 2 & (dd < 30)) & LeapYear == true) { 
      System.out.println("You have entered an invalid day for the month of February."); 
     } else { 
      System.out.println("You have entered a valid date in the correct format."); 
     } 

     /** 
     * Leap Year for February but with correct day. 
     */ 
     if (LeapYear) { 
      if ((mm == 2 & (dd == 29)) & LeapYear == true) { 
       System.out.println(date + " is a valid date."); 
      } 
     } else { 
      System.out.println(date + "is not valid month must have 29 days or less."); 
     } 
     if ((mm == 2) && (dd <= 28)) { 
      System.out.println(date + " is a valid date."); 
     } 

    } 
} 
+0

你讀日期爲一個字符串,但沒有用它做什麼... –

+0

好了,你有一個叫做'date'字符串。你在做什麼把這些值放在'mm','dd'或'yyyy'中? –

+1

你的'mm','dd'和'yyyy'總是*爲零;因爲這就是你設定的(兩次)。 –

回答

0

從上面我的意見:爲你的程序目前,你date閱讀,但你不要用它做任何事情。

此外,你有意嘗試手動解析日期嗎?難道不足以做類似

try { 
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); 
    Date date = df.parse(dateString); 
} catch (ParseException e) { 
    // incorrect date 
} 

編輯(見註釋部分):

try { 
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
    Date date = df.parse(dateString); 
} catch (...) { 
    ... 
} 
+0

這不適用於_my_語言環境。 –

+0

@DavidWallace那麼,在這種情況下,你可以使用'SimpleDateFormat'並指定你想要的格式。 –

+0

@DavidWallace¯\\ _(ツ)_ /¯這個問題很少有我根據我給出的答案回答的信息。 –

0

你的價值永遠不會設置爲您要求用戶輸入。您需要從用戶那裏獲取輸入,然後將其解析爲片段,然後您可以調用Integer.parseInt將字符串表示轉換爲整數。

試試這個:

//check to see if the input recieved is ##/##/#### 
if(date.matches("\\d\\d/\\d\\d/\\d\\d\\d\\d")) 
{ 
    //now split the string around/to get the 
    //numbers you need for your checks 
    String[] pieces = date.split("/"); 
    mm = Integer.parseInt(pieces[0]); 
    dd = Integer.parseInt(pieces[1]); 
    yyyy = Integer.parseInt(pieces[2]); 
    //now do your logic checks based on the numbers that the user typed 

} 
//the user did not enter the correct format of input 
else 
{ 
    System.out.println("Sorry Incorrect Input Format") 
} 
0
Scanner keyboard = new Scanner(System.in).useDelimiter("/"); 

int mm = keyboard.nextInt(); 
int dd = keyboard.nextInt(); 
int yyyy = keyboard.nextInt(); 

// etc 
+1

我試過了。它不起作用。我的'掃描儀'仍然在等待一年後出現'/'字符。您需要將分隔符設爲_either_'/'_或_行尾。 –