i am writing a program which involves taking the date of birth of users..so what i have done is :
1> first ask the user to input year(check if it is between 1900 and 3000)
2>then ask the user to input month(check if it is between 1 to 12).
3>finally ask him to enter the date(check if in the range between 1 - 30 or 1-31(based on the month));
4> then concatenate in dd/mm/yyyy format( using "+" operator for string concatenation) and store into the String variable dob
i was wondering if there was any better way to do it..like if a user inputs some char when scanner.nextInt() is waiting to read an integer, an exception is thrown..is there something similar where the user can be asked to input the date in some format like dd/mm/yyyy and if the input format is incorrect, throw an exception like inputMismatchException... please help.
couldn't you ask them to input a date in the format dd/mm/yyyy, then either use a split or else substring etc and valid and it can print out where they were out of range and if so make them re-enter.
Joined: Feb 11, 2012
Wesleigh Pieters wrote:couldn't you ask them to input a date in the format dd/mm/yyyy, then either use a split or else substring etc and valid and it can print out where they were out of range and if so make them re-enter.
hey thank you for the idea.... ill make use of it..but i was wondering if i could make the compiler check if the input is in a valid date format or not..like ensuring somebody doesn't type their dob as 31 february..or something like that..of course coding can be used for such validation..is there any existing class i can make use of?
Joined: Sep 04, 2012
I just did some googling, If you get an instance of Calender, set its leniency to false then when you retrieve it it will throw an exception if it is invalid. I havent tried it myself though.