2016-09-28 65 views
0

我被要求製作一個程序,它會接收用戶輸入的日期並檢查幾個標準:日期長度,閏年,月份準確性,日期準確性等。 。我相信我已經得到了一切想通了,除了當我測試的日期「12/345/678」我得到了一系列的Java錯誤包括:驗證日期輸入 - 修復java.lang.numberformatexception錯誤

Exception in thread "main" java.lang.NumberFormatException: For input string: "/678" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at DateChecker.main(DateChecker.java:42) 

我認爲,這是因爲當java的嘗試提取「yearString」時,它無法使用,因爲它已經使用intparser轉換爲整數,並且輸入的代碼現在包含「/」,所以與日期「1/234/5678」會發生相同的錯誤,因爲monthString正在查找所有整數,但它現在包含一個「/」。

我知道下面的代碼,因爲我已經通過另一個類似的問題閱讀了另一個StackOverflow文章,雖然當我試圖將它應用到我的程序時,我得到了一系列的編譯器錯誤,因爲我的變量現在被關閉了程序

try{ 
    int i = Integer.parseInt(input); 
}catch(NumberFormatException ex){ // handle your exception 
    ... 
} 

這裏休息是我到目前爲止的代碼,並在此先感謝所提供:)

import java.util.*; 

public class DateChecker { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); //activates the keyboard 

     String dateString; //variable declaration 

     System.out.println("Please enter a date in the format (mm/dd/yyyy): "); //prints to screen 
     dateString = keyboard.nextLine();//user enters dateString value 

     if (dateString.length()>10)//checks if date is greater than 10 
     { 
      System.out.println("Invalid date."); //prints to screen 
      System.out.println("To many characters in the date.");//prints to screen 
     } 
     else if(dateString.length()<10)//checks if date is less than 10 
     { 
      System.out.println("Invalid date.");//prints to screen 
      System.out.println("To few characters in the date.");//prints to screen 
     } 
     else {//date = 10 
      if(dateString.charAt(2)!='/' && dateString.charAt(5)!='/')//checks for "/" at spots 2 and 5 
      { 
       System.out.println("Invalid date.");//prints to screen 
       System.out.println("Incorrect format.");//prints to screen 
      } 
      else{//"/" at spots 2 and 5 
       //declares variables and extracts strings 
       String yearString = dateString.substring(6, 10); 
       String dayString = dateString.substring(3, 5); 
       String monthString = dateString.substring(0, 2); 

       //converts string variables to integer 
       int monthInt=Integer.parseInt(monthString); 
       int dayInt=Integer.parseInt(dayString); 
       int yearInt=Integer.parseInt(yearString); 

       if(monthInt < 1|| monthInt > 12)//checks if valid month is entered 
       { 
        System.out.println("Invalid date.");//prints to screen 
        System.out.println("Month is not valid.");//prints to screen 
       } 
       else 
       {//month is valid 
        if(dayInt < 1) 
        { 
         System.out.println("Invalid date."); 
         System.out.println("Day is not valid."); 
        } 

        else { 
         if((monthInt == 4 || monthInt == 6 || monthInt == 9 || monthInt == 11) && dayInt > 30)//checks if months should have 30 days 
         { 
          System.out.println("Invalid date.");//prints to screen 
          System.out.println("Day is not valid.");//prints to screen 
         } 
         else 
          if (yearInt % 4 ==0 && (monthInt == 2 && dayInt > 29))//checks if leap year 
          { 
           System.out.println("Invalid date.");//prints to screen 
           System.out.println("Day is not valid.");//prints to screen 
          } 

          else//if not leap year 
           if (yearInt % 4 != 0 && dayInt > 28)//checks if normal year 
           { 
            System.out.println("Invalid date.");//prints to screen 
            System.out.println("Day is not valid.");//prints to screen 
           } 

           else //date entered was valid 
           { 
            System.out.println("Valid date."); 
           } 
        } 
       } 
      }     


     }    
    } 
} 
+1

使用java日期類來解析date.you不需要你自己做這一切。 –

+1

我不明白你爲什麼要這麼做。如果DateFormatter失敗,那麼這是一個無效日期 - 故事結束。 –

+0

你不能用if語句捕捉所有可能的輸入錯誤。這就是存在異常捕獲的原因。另外,詳細的消息很好,但是過分。 「無效的日期」可能是足夠的錯誤消息。 –

回答

1

在你的代碼類的問題就在這裏: -

if(dateString.charAt(2)!='/' && dateString.charAt(5)!='/') //checks for "/" at spots 2 and 5 
{ 
    System.out.println("Invalid date.");//prints to screen 
    System.out.println("Incorrect format.");//prints to screen 
} 

在這裏,你正在尋找兩個條件是真實的,而不是你應該使用來代替

這個你需要做一個小的變化: -

if(dateString.charAt(2)!='/' || dateString.charAt(5)!='/') //checks for "/" at spots 2 and 5 
{ 
    System.out.println("Invalid date.");//prints to screen 
    System.out.println("Incorrect format.");//prints to screen 
} 

我會建議你使用由JAVA

定的Date類謝謝你, 快樂幫:)

1

你或許應該使用類似的點DateTimeFormatter.parse(CharSequence)情況下,任何的幫助是你的規則eap年是不正確的。如果年份可以被100整除,那麼它不是閏年,儘管年份可以被4除盡,除非它也可以被400整除。在這種情況下,它是一個閏年。

日期是很難,你頭到時區的土地,甚至之前...

+0

在這個問題上沒有時間 –

+0

@ScaryWombat你是對的,如果你沒有對日期做任何其他事情,直接使用DateTimeFormatter會更好...更改我的答案以反映這一點。 –

0

如何使用已經存在的

System.out.println("Please enter a date in the format (mm/dd/yyyy): "); //prints to screen 
String dateString = keyboard.nextLine();//user enters dateString value 

SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy"); 

try { 
    sdf.parse (dateString); 
} catch (ParseException ex) { 
    System.out.println("Invalid date.");//prints to screen 
}