我被要求製作一個程序,它會接收用戶輸入的日期並檢查幾個標準:日期長度,閏年,月份準確性,日期準確性等。 。我相信我已經得到了一切想通了,除了當我測試的日期「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.");
}
}
}
}
}
}
}
使用java日期類來解析date.you不需要你自己做這一切。 –
我不明白你爲什麼要這麼做。如果DateFormatter失敗,那麼這是一個無效日期 - 故事結束。 –
你不能用if語句捕捉所有可能的輸入錯誤。這就是存在異常捕獲的原因。另外,詳細的消息很好,但是過分。 「無效的日期」可能是足夠的錯誤消息。 –