Results 1 to 6 of 6

Thread: date validation

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    7

    Default 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();
    	}
    
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Quote Originally Posted by droidus View Post
    any ideas as to what is wrong with my loop?
    What loop? There isn't one in the code you posted.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    7

    Default

    the else that acts like it, if the credentials aren't entered properly.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Quote Originally Posted by droidus View Post
    the else that acts like it, if the credentials aren't entered properly.
    um, no it doesn't. If you need a loop, use a loop.

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    7

    Default

    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) <<<
    Attached Thumbnails Attached Thumbnails date validation-java_help.jpg  
    Last edited by droidus; 03-16-2011 at 10:55 PM.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    259
    Rep Power
    7

Similar Threads

  1. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  2. License date validation
    By thaneshkadi in forum Forum Lobby
    Replies: 0
    Last Post: 10-23-2009, 12:26 AM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Date Validation Script
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •