Results 1 to 6 of 6
Thread: date validation
- 03-15-2011, 11:25 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
date validation
the purpose of this code is to check and see if the date is valid. this is only the first part. i am stuck, because if i enter an invalid date, it only goes through twice, then ends (and doesn't output anything...). if i enter a valid one, it works perfectly fine! any ideas as to what is wrong with my loop?
Java Code:import java.util.Scanner; public class DateValidationConversion { public static void main(String[]args) { String curDate = ""; char firstDateValidation = ' '; char nextDateValidation = ' '; String curMonth = ""; String curDay = ""; boolean validDate = false; int dateLength = 0; //Prompts user for the date Scanner in = new Scanner(System.in); System.out.println("Please enter the current date in MM/DD/YYYY format (including 0's): "); curDate = in.next(); dateLength = curDate.length(); firstDateValidation = curDate.charAt(2); nextDateValidation = curDate.charAt(5); if (firstDateValidation == '/' && nextDateValidation == '/' && dateLength == 10) { validDate = true; curMonth = curDate.substring(0,2); curDay = curDate.substring(3,5); System.out.println ("This is a valid date."); if (curMonth.equals("02") && curDay.equals("29")) { System.out.println("This IS a leap year!"); } } else { //Prompts user for the date System.out.println("Please enter the current date in MM/DD/YYYY format (including 0's): "); curDate = in.next(); } } }
- 03-15-2011, 11:33 PM #2
- 03-16-2011, 01:24 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
the else that acts like it, if the credentials aren't entered properly.
-
- 03-16-2011, 02:42 PM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
i am getting a weird error here, and am not sure what it means...
Please enter the current date in MM/DD/YYYY format (including 0's):
10/11/1992
10/11/1992 is a valid date.
Exception in thread "main" java.lang.NumberFormatException: For input string: " "
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at DateValidationConversion.main(DateValidationConver sion.java:83) <<<Last edited by droidus; 03-16-2011 at 09:55 PM.
- 03-19-2011, 01:37 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Similar Threads
-
converting string (GMT date) to date in US time.
By JRuyechan in forum New To JavaReplies: 1Last Post: 10-15-2010, 07:07 AM -
License date validation
By thaneshkadi in forum Forum LobbyReplies: 0Last Post: 10-23-2009, 12:26 AM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Date Validation Script
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks